Module: React::FunctionComponent::NativeComponentConstructor

Defined in:
lib/react/function_component/native_component_constructor.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/react/function_component/native_component_constructor.rb', line 4

def self.extended(base)
  component_name = base.to_s
  %x{
    base.instance_init = function(initial) { return { instance: #{base.new(`{}`)} }; }
    base.instance_reducer = function(state, action) { return state; }
    base.react_component = function(props) {
      const oper = Opal.React;
      oper.render_buffer.push([]);
      // console.log("function pushed", oper.render_buffer, oper.render_buffer.toString());
      const [__ruby_state, __ruby_dispatch] = Opal.global.React.useReducer(base.instance_reducer, null, base.instance_init);
      const __ruby_instance = __ruby_state.instance;
      __ruby_instance.props = props;
      oper.active_components.push(__ruby_instance);
      let block_result = #{`__ruby_instance`.instance_exec(&`base.render_block`)};
      if (block_result && (block_result.constructor === String || block_result.constructor === Number)) { oper.render_buffer[oper.render_buffer.length - 1].push(block_result); }
      oper.active_components.pop();
      // console.log("function popping", oper.render_buffer, oper.render_buffer.toString());
      return oper.render_buffer.pop();
    }
    base.react_component.displayName = #{component_name};
  }

  base_module = base.to_s.deconstantize
  if base_module != ''
    base_module.constantize.define_singleton_method(base.to_s.demodulize) do |*args, &block|
      `Opal.React.internal_prepare_args_and_render(#{base}.react_component, args, block)`
    end
  else
    Object.define_method(base.to_s) do |*args, &block|
      `Opal.React.internal_prepare_args_and_render(#{base}.react_component, args, block)`
    end
  end

  def render(&block)
    `base.render_block = #{block}`
  end
end

Instance Method Details

#render(&block) ⇒ Object



37
38
39
# File 'lib/react/function_component/native_component_constructor.rb', line 37

def render(&block)
  `base.render_block = #{block}`
end