module React::FunctionComponent::Api

Public Instance Methods

get_react_element(arg, &block) click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 60
def get_react_element(arg, &block)
  if block_given?
    # execute block, fetch last element from buffer
    %x{
      let last_buffer_length = Opal.React.render_buffer[Opal.React.render_buffer.length - 1].length;
      let last_buffer_element = Opal.React.render_buffer[Opal.React.render_buffer.length - 1][last_buffer_length - 1];
      block.$call();
      // console.log("get_react_element popping", Opal.React.render_buffer, Opal.React.render_buffer.toString())
      let new_element = Opal.React.render_buffer[Opal.React.render_buffer.length - 1].pop();
      if (last_buffer_element === new_element) { #{Isomorfeus.raise_error(message: "Block did not create any React element!")} }
      return new_element;
    }
  else
    # element was rendered before being passed as arg
    # fetch last element from buffer
    # `console.log("get_react_element popping", Opal.React.render_buffer, Opal.React.render_buffer.toString())`
    `Opal.React.render_buffer[Opal.React.render_buffer.length - 1].pop()`
  end
end
Also aliased as: gre
gre(arg, &block)
Alias for: get_react_element
m_ref(method_symbol, *args)
Alias for: method_ref
method_ref(method_symbol, *args) click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 89
def method_ref(method_symbol, *args)
  method_key = "#{method_symbol}#{args}"
  %x{
    if (#{self}.method_refs && #{self}.method_refs[#{method_key}]) { return #{self}.method_refs[#{method_key}]; }
    if (!#{self}.method_refs) { #{self}.method_refs = {}; }
    #{self}.method_refs[#{method_key}] = { m: #{method(method_symbol)}, a: args };
    return #{self}.method_refs[#{method_key}];
  }
end
Also aliased as: m_ref
props() click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 4
def props
  @native_props
end
render_react_element(el) click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 81
def render_react_element(el)
  # push el to buffer
  `Opal.React.render_buffer[Opal.React.render_buffer.length - 1].push(el)`
  # `console.log("render_react_element pushed", Opal.React.render_buffer, Opal.React.render_buffer.toString())`
  nil
end
Also aliased as: rre
rre(el)
to_n() click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 100
def to_n
  self
end
use_callback(deps, &block) click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 8
def use_callback(deps, &block)
  `Opal.global.React.useCallback(function() { #{block.call} }, deps)`
end
use_context(context) click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 12
def use_context(context)
  `(typeof context.$is_wrapped_context !== 'undefined')` ? context.to_n : context
  `Opal.global.React.useContext(native_context)`
end
use_debug_value(value) click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 17
def use_debug_value(value)
  `Opal.global.React.useDebugValue(value)`
end
use_effect(*args, &block) click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 21
def use_effect(*args, &block)
  `Opal.global.React.useEffect(function() { #{block.call} }, args)`
end
use_imperative_handle(ref, *deps, &block) click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 25
def use_imperative_handle(ref, *deps, &block)
  native_ref = `(typeof ref.$is_wrapped_ref !== 'undefined')` ? ref.to_n : ref
  `Opal.global.React.useImperativeHandle(native_ref, function() { #{block.call} }, deps)`
end
use_layout_effect(&block) click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 30
def use_layout_effect(&block)
  `Opal.global.React.useLayoutEffect(function() { #{block.call} })`
end
use_memo(*deps, &block) click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 34
def use_memo(*deps, &block)
  `Opal.global.React.useMemo(function() { #{block.call} }, deps)`
end
use_reducer(inital_state, &block) click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 38
def use_reducer(inital_state, &block)
  state = nil
  dispatcher = nil
  %x{
    [state, dispatcher] = Opal.global.React.useReducer(function(state, action) {
      #{block.call(state, action)}
    }, initial_state);
  }
  [state, proc { |arg| `dispatcher(arg)` }]
end
use_ref(native_ref) click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 49
def use_ref(native_ref)
  React::Ref.new(`Opal.global.React.useRef(native_ref)`)
end
use_state(initial_value) click to toggle source
# File lib/isomorfeus_react/react/function_component/api.rb, line 53
def use_state(initial_value)
  initial = nil
  setter = nil
  `[initial, setter] = Opal.global.React.useState(initial_value);`
  [initial, proc { |arg| `setter(arg)` }]
end