class React::Component

Public Class Methods

default_props() click to toggle source
# File lib/opal/react/component.rb, line 133
def self.default_props
  nil
end
dsl_name(name) click to toggle source
# File lib/opal/react/component.rb, line 125
def self.dsl_name(name)
  name.gsub( /Component$/, '')
end
inherited(subclass) click to toggle source
Calls superclass method
# File lib/opal/react/component.rb, line 47
def self.inherited(subclass)
  super
  _class = `
    class extends React.Component {

      static get defaultProps() {
        return #{React.fix_props(`#{subclass}.$default_props()`)};
      }

      constructor(props) {
        super(props);
        this.self = #{subclass.new(`props`)};
        this.state = #{React.fix_state(`this.self.$initial_state()`)};
      }

      render() {
        return this.self.$bridge(this).self.$render();
      }

      componentWillMount() {
        this.self.$bridge(this).self.$component_will_mount();
      }

      componentDidMount() {
        this.self.$bridge(this).self.$component_did_mount();
      }

      componentWillReceiveProps(nextProps) {
        this.self.$bridge(this).self.$component_will_receive_props(nextProps);
      }

      shouldComponentUpdate(nextProps, nextState) {
        return ("$should_component_update?" in this.self)
          ? this.self.$bridge(this).self["$should_component_update?"](nextProps, nextState)
          : shallowCompare(this, nextProps, nextState);
      }

      componentWillUpdate(nextProps, nextState) {
        this.self.$bridge(this).self.$component_will_update(nextProps, nextState);
      }

      componentDidUpdate(prevProps, prevState) {
        this.self.$bridge(this).self.$component_did_update(prevProps, prevState);
      }

      componentWillUnmount() {
        this.self.$bridge(this).self.$component_will_unmount();
      }
    }
  `

  _create = Proc.new { |props = nil, children = nil, &block|
    if children.nil?
      if `Array.isArray(props)`
        children = props
        props = nil
      elsif !props.nil? && `typeof props !== 'object'`
        children = [props]
        props = nil
      end
    end
    `return React.createElement( #{_class}, #{React.fix_props(props)}, #{children.to_n} );`
  }

  name = subclass.dsl_name(subclass.name)
  DSL.define_method(name, _create)

  subclass.class.define_method('run') do |props = nil, container = nil|
    if container
      ReactDOM.render(_create.call(props), container)
    else
      ReactDOM.render(_create.call(props), container = $$.document.createElement('div')) do
        $$.document.body.appendChild(container)
      end
    end
  end
end

Public Instance Methods

bridge(value) click to toggle source
# File lib/opal/react/component.rb, line 129
def bridge(value)
  @this = value
end
component_did_mount() click to toggle source
# File lib/opal/react/component.rb, line 148
def component_did_mount()
end
component_did_update(next_props, next_state) click to toggle source
# File lib/opal/react/component.rb, line 161
def component_did_update(next_props, next_state)
end
component_will_mount() click to toggle source
# File lib/opal/react/component.rb, line 145
def component_will_mount()
end
component_will_receive_props(next_props) click to toggle source
# File lib/opal/react/component.rb, line 151
def component_will_receive_props(next_props)
end
component_will_unmount() click to toggle source
# File lib/opal/react/component.rb, line 164
def component_will_unmount()
end
component_will_update(next_props, next_state) click to toggle source

def should_component_update?(next_props, next_state)

true

end

# File lib/opal/react/component.rb, line 158
def component_will_update(next_props, next_state)
end
force_update() click to toggle source
# File lib/opal/react/component.rb, line 188
def force_update
  `#{@this}.forceUpdate();`
end
initial_state() click to toggle source
# File lib/opal/react/component.rb, line 137
def initial_state
  nil
end
mounted?() click to toggle source

deprecated (more or less)

# File lib/opal/react/component.rb, line 193
def mounted?
  `return #{@this}.isMounted();`
end
prop_types() click to toggle source
# File lib/opal/react/component.rb, line 141
def prop_types
  nil
end
props() click to toggle source
# File lib/opal/react/component.rb, line 170
def props
  ShallowWrapper.new(`#{@this}.props`)
end
render() click to toggle source
# File lib/opal/react/component.rb, line 167
def render()
end
replace_state(next_state, &block) click to toggle source
# File lib/opal/react/component.rb, line 183
def replace_state(next_state, &block)
  block = `null` if block.nil?
  `#{@this}.replaceState(#{React.fix_state(next_state)}, block);`
end
set_state(next_state, &block) click to toggle source
# File lib/opal/react/component.rb, line 178
def set_state(next_state, &block)
  block = `null` if block.nil?
  `#{@this}.setState(#{React.fix_state(next_state)}, block);`
end
state() click to toggle source
# File lib/opal/react/component.rb, line 174
def state
  ShallowWrapper.new(`#{@this}.state`)
end