Module: React

Defined in:
lib/react.rb,
lib/react/ref.rb,
lib/react/version.rb,
lib/react/children.rb,
lib/react/component/api.rb,
lib/react/vivify_module.rb,
lib/react/component/base.rb,
lib/react/component/match.rb,
lib/react/component/mixin.rb,
lib/react/component/props.rb,
lib/react/component/state.rb,
lib/react/context_wrapper.rb,
lib/react/synthetic_event.rb,
lib/react/component/styles.rb,
lib/react/component/history.rb,
lib/react/component/elements.rb,
lib/react/component/features.rb,
lib/react/component/location.rb,
lib/react/component/callbacks.rb,
lib/react/memo_component/base.rb,
lib/react/component/resolution.rb,
lib/react/component/unsafe_api.rb,
lib/react/memo_component/mixin.rb,
lib/react/component/initializer.rb,
lib/react/function_component/api.rb,
lib/react/component/event_handler.rb,
lib/react/function_component/base.rb,
lib/react/native_constant_wrapper.rb,
lib/react/function_component/mixin.rb,
lib/react/function_component/resolution.rb,
lib/react/function_component/initializer.rb,
lib/react/function_component/event_handler.rb,
lib/react/component/native_component_constructor.rb,
lib/react/memo_component/native_component_constructor.rb,
lib/react/function_component/native_component_constructor.rb

Defined Under Namespace

Modules: Children, Component, FunctionComponent, MemoComponent Classes: ContextWrapper, NativeConstantWrapper, Ref, SyntheticEvent, VivifyModule

Constant Summary collapse

VERSION =
'16.11.3'

Class Method Summary collapse

Class Method Details

.clone_element(ruby_react_element, props = nil, children = nil, &block) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/react.rb', line 106

def self.clone_element(ruby_react_element, props = nil, children = nil, &block)
  block_result = `null`
  if block_given?
    block_result = block.call
    block_result = `null` unless block_result
  end
  native_props = props ? `Opal.React.to_native_react_props(props)` : `null`
  `Opal.global.React.cloneElement(ruby_react_element.$to_n(), native_props, block_result)`
end

.create_context(const_name, default_value) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/react.rb', line 116

def self.create_context(const_name, default_value)
  %x{
    Opal.global[const_name] = Opal.global.React.createContext(default_value);
    var new_const = #{React::ContextWrapper.new(`Opal.global[const_name]`)};
    #{Object.const_set(const_name, `new_const`)};
    return new_const;
  }
end

.create_element(type, props = nil, children = nil, &block) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/react.rb', line 125

def self.create_element(type, props = nil, children = nil, &block)
  %x{
    let component = null;
    let native_props = null;
    if (typeof type.react_component !== 'undefined') { component = type.react_component; }
    else { component = type; }
    if (block !== nil) {
      Opal.React.render_buffer.push([]);
      // console.log("create_element 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("create_element 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; }
    } else if (children === nil) { children = null; }
    if (props && props !== nil) { native_props = Opal.React.to_native_react_props(props); }
    return Opal.global.React.createElement(component, native_props, children);
  }
end

.create_factory(type) ⇒ Object



148
149
150
151
# File 'lib/react.rb', line 148

def self.create_factory(type)
  native_function = `Opal.global.React.createFactory(type)`
  proc { `native_function.call()` }
end

.create_refObject



153
154
155
# File 'lib/react.rb', line 153

def self.create_ref
  React::Ref.new(`Opal.global.React.createRef()`)
end

.forwardRef(&block) ⇒ Object



157
158
159
160
# File 'lib/react.rb', line 157

def self.forwardRef(&block)
  # TODO whats the return here? A React:Element?, doc says a React node, whats that?
  `Opal.global.React.forwardRef( function(props, ref) { return block.$call().$to_n(); })`
end

.is_valid_element(react_element) ⇒ Object



162
163
164
# File 'lib/react.rb', line 162

def self.is_valid_element(react_element)
  `Opal.global.React.isValidElement(react_element)`
end

.lazy(import_statement_function) ⇒ Object



166
167
168
# File 'lib/react.rb', line 166

def self.lazy(import_statement_function)
  `Opal.global.React.lazy(import_statement_function)`
end

.memo(function_component, &block) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/react.rb', line 170

def self.memo(function_component, &block)
  if block_given?
    %x{
      var fun = function(prev_props, next_props) {
        return #{block.call(::React::Component::Props.new(`{props: prev_props}`), ::React::Component::Props.new(`{props: next_props}`))};
      }
      return Opal.global.React.memo(function_component, fun);
    }
  else
    `Opal.global.React.memo(function_component)`
  end
end