Class: React::ContextWrapper

Inherits:
Object
  • Object
show all
Includes:
Native::Wrapper
Defined in:
lib/react/context_wrapper.rb

Instance Method Summary collapse

Instance Method Details

#Consumer(*args, &block) ⇒ Object



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
# File 'lib/react/context_wrapper.rb', line 9

def Consumer(*args, &block)
  # why not use internal_prepare_args and render?
  %x{
    let props = null;

    if (args.length > 0) { props = Opal.React.to_native_react_props(args[0]); }

    let react_element = Opal.global.React.createElement(this.native.Consumer, props, function(value) {
      let children = null;
      if (block !== nil) {
        Opal.React.render_buffer.push([]);
        // console.log("consumer pushed", Opal.React.render_buffer, Opal.React.render_buffer.toString());
        let block_result = block.$call();
        if (block_result && (block_result.constructor === String || block_result.constructor === Number)) {
          Opal.React.render_buffer[Opal.React.render_buffer.length - 1].push(block_result);
        }
        // console.log("consumer popping", Opal.React.render_buffer, Opal.React.render_buffer.toString());
        children = Opal.React.render_buffer.pop();
        if (children.length == 1) { children = children[0]; }
        else if (children.length == 0) { children = null; }
      }
      return children;
    });
    Opal.React.render_buffer[Opal.React.render_buffer.length - 1].push(react_element);
  }
end

#is_wrapped_contextObject



5
6
7
# File 'lib/react/context_wrapper.rb', line 5

def is_wrapped_context
  true
end

#Provider(*args, &block) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/react/context_wrapper.rb', line 36

def Provider(*args, &block)
  # why not use internal_prepare_args and render?
  %x{
    var props = null;
    if (args.length > 0) { props = Opal.React.to_native_react_props(args[0]); }
    Opal.React.internal_render(this.native.Provider, props, null, block);
  }
end