module React::Component::Callbacks

Public Class Methods

included(base) click to toggle source
# File lib/react/component/callbacks.rb, line 4
def self.included(base)
  base.instance_exec do
    def component_did_catch(&block)
      # TODO convert error and info
      %x{
        var fun = function(error, info) {
          Opal.React.active_redux_components.push(this);
          #{`this.__ruby_instance`.instance_exec(`error`, `info`, &block)};
          Opal.React.active_redux_components.pop();
        }
        if (self.lucid_react_component) { self.lucid_react_component.prototype.componentDidCatch = fun; }
        else { self.react_component.prototype.componentDidCatch = fun; }
      }
    end

    def component_did_mount(&block)
      %x{
        let fun = function() {
          Opal.React.active_redux_components.push(this);
          #{`this.__ruby_instance`.instance_exec(&block)};
          Opal.React.active_redux_components.pop();
        }
        if (self.lucid_react_component) {
          if (self.lucid_react_component.prototype.componentDidMount) {
            let fun_one = self.lucid_react_component.prototype.componentDidMount;
            let fun_two = fun;
            fun = function() {
              fun_one();
              fun_two();
            }
          }
          self.lucid_react_component.prototype.componentDidMount = fun;
        } else { self.react_component.prototype.componentDidMount = fun; }
      }
    end

    def component_did_update(&block)
      %x{
        var fun = function(prev_props, prev_state, snapshot) {
          Opal.React.active_redux_components.push(this);
          #{`this.__ruby_instance`.instance_exec(`Opal.React.Component.Props.$new({props: prev_props})`,
                                                 `Opal.React.Component.State.$new({state: prev_state})`,
                                                 `snapshot`, &block)};
          Opal.React.active_redux_components.pop();
        }
        if (self.lucid_react_component) { self.lucid_react_component.prototype.componentDidUpdate = fun; }
        else { self.react_component.prototype.componentDidUpdate = fun; }
      }
    end

    def component_will_unmount(&block)
      %x{
        var fun = function() {
          if (typeof this.unsubscriber === "function") { this.unsubscriber(); };
          Opal.React.active_redux_components.push(this);
          #{`this.__ruby_instance`.instance_exec(&block)};
          Opal.React.active_redux_components.pop();
        }
        if (self.lucid_react_component) { self.lucid_react_component.prototype.componentWillUnmount = fun; }
        else { self.react_component.prototype.componentWillUnmount = fun; }
      }
    end

    def get_derived_state_from_error(&block)
      # TODO convert error
      %x{
         var fun = function(error) {
          var result = #{`this.__ruby_instance`.instance_exec(`error`, &block)};
          if (typeof result.$to_n === 'function') { result = result.$to_n() }
          if (result === Opal.nil) { return null; }
          return result;
        }
        if (self.lucid_react_component) { self.lucid_react_component.prototype.getDerivedStateFromError = fun; }
        else { self.react_component.prototype.getDerivedStateFromError = fun; }
      }
    end

    def get_derived_state_from_props(&block)
      %x{
        var fun = function(props, state) {
          Opal.React.active_redux_components.push(this);
          var result = #{`this.__ruby_instance`.instance_exec(`Opal.React.Component.Props.$new({props: props})`,
                                                               `Opal.React.Component.State.$new({state: state})`, &block)};
          Opal.React.active_redux_components.pop();
          if (typeof result.$to_n === 'function') { result = result.$to_n() }
          if (result === Opal.nil) { return null; }
          return result;
        }
        if (self.lucid_react_component) { self.lucid_react_component.prototype.getDerivedStateFromProps = fun; }
        else { self.react_component.prototype.getDerivedStateFromProps = fun; }
      }
    end

    def get_snapshot_before_update(&block)
      %x{
        var fun = function(prev_props, prev_state) {
          Opal.React.active_redux_components.push(this);
          var result = #{`this.__ruby_instance`.instance_exec(`Opal.React.Component.Props.$new({props: prev_props})`,
                                                              `Opal.React.Component.State.$new({state: prev_state})`, &block)};
          Opal.React.active_redux_components.pop();
          if (result === Opal.nil) { return null; }
          return result;
        }
        if (self.lucid_react_component) { self.lucid_react_component.prototype.getSnapshotBeforeUpdate = fun; }
        else { self.react_component.prototype.getSnapshotBeforeUpdate = fun; }
      }
    end
  end
end

Public Instance Methods

component_did_catch(&block) click to toggle source
# File lib/react/component/callbacks.rb, line 6
def component_did_catch(&block)
  # TODO convert error and info
  %x{
    var fun = function(error, info) {
      Opal.React.active_redux_components.push(this);
      #{`this.__ruby_instance`.instance_exec(`error`, `info`, &block)};
      Opal.React.active_redux_components.pop();
    }
    if (self.lucid_react_component) { self.lucid_react_component.prototype.componentDidCatch = fun; }
    else { self.react_component.prototype.componentDidCatch = fun; }
  }
end
component_did_mount(&block) click to toggle source
# File lib/react/component/callbacks.rb, line 19
def component_did_mount(&block)
  %x{
    let fun = function() {
      Opal.React.active_redux_components.push(this);
      #{`this.__ruby_instance`.instance_exec(&block)};
      Opal.React.active_redux_components.pop();
    }
    if (self.lucid_react_component) {
      if (self.lucid_react_component.prototype.componentDidMount) {
        let fun_one = self.lucid_react_component.prototype.componentDidMount;
        let fun_two = fun;
        fun = function() {
          fun_one();
          fun_two();
        }
      }
      self.lucid_react_component.prototype.componentDidMount = fun;
    } else { self.react_component.prototype.componentDidMount = fun; }
  }
end
component_did_update(&block) click to toggle source
# File lib/react/component/callbacks.rb, line 40
def component_did_update(&block)
  %x{
    var fun = function(prev_props, prev_state, snapshot) {
      Opal.React.active_redux_components.push(this);
      #{`this.__ruby_instance`.instance_exec(`Opal.React.Component.Props.$new({props: prev_props})`,
                                             `Opal.React.Component.State.$new({state: prev_state})`,
                                             `snapshot`, &block)};
      Opal.React.active_redux_components.pop();
    }
    if (self.lucid_react_component) { self.lucid_react_component.prototype.componentDidUpdate = fun; }
    else { self.react_component.prototype.componentDidUpdate = fun; }
  }
end
component_will_unmount(&block) click to toggle source
# File lib/react/component/callbacks.rb, line 54
def component_will_unmount(&block)
  %x{
    var fun = function() {
      if (typeof this.unsubscriber === "function") { this.unsubscriber(); };
      Opal.React.active_redux_components.push(this);
      #{`this.__ruby_instance`.instance_exec(&block)};
      Opal.React.active_redux_components.pop();
    }
    if (self.lucid_react_component) { self.lucid_react_component.prototype.componentWillUnmount = fun; }
    else { self.react_component.prototype.componentWillUnmount = fun; }
  }
end
get_derived_state_from_error(&block) click to toggle source
# File lib/react/component/callbacks.rb, line 67
def get_derived_state_from_error(&block)
  # TODO convert error
  %x{
     var fun = function(error) {
      var result = #{`this.__ruby_instance`.instance_exec(`error`, &block)};
      if (typeof result.$to_n === 'function') { result = result.$to_n() }
      if (result === Opal.nil) { return null; }
      return result;
    }
    if (self.lucid_react_component) { self.lucid_react_component.prototype.getDerivedStateFromError = fun; }
    else { self.react_component.prototype.getDerivedStateFromError = fun; }
  }
end
get_derived_state_from_props(&block) click to toggle source
# File lib/react/component/callbacks.rb, line 81
def get_derived_state_from_props(&block)
  %x{
    var fun = function(props, state) {
      Opal.React.active_redux_components.push(this);
      var result = #{`this.__ruby_instance`.instance_exec(`Opal.React.Component.Props.$new({props: props})`,
                                                           `Opal.React.Component.State.$new({state: state})`, &block)};
      Opal.React.active_redux_components.pop();
      if (typeof result.$to_n === 'function') { result = result.$to_n() }
      if (result === Opal.nil) { return null; }
      return result;
    }
    if (self.lucid_react_component) { self.lucid_react_component.prototype.getDerivedStateFromProps = fun; }
    else { self.react_component.prototype.getDerivedStateFromProps = fun; }
  }
end
get_snapshot_before_update(&block) click to toggle source
# File lib/react/component/callbacks.rb, line 97
def get_snapshot_before_update(&block)
  %x{
    var fun = function(prev_props, prev_state) {
      Opal.React.active_redux_components.push(this);
      var result = #{`this.__ruby_instance`.instance_exec(`Opal.React.Component.Props.$new({props: prev_props})`,
                                                          `Opal.React.Component.State.$new({state: prev_state})`, &block)};
      Opal.React.active_redux_components.pop();
      if (result === Opal.nil) { return null; }
      return result;
    }
    if (self.lucid_react_component) { self.lucid_react_component.prototype.getSnapshotBeforeUpdate = fun; }
    else { self.react_component.prototype.getSnapshotBeforeUpdate = fun; }
  }
end