Skip to content

Instantly share code, notes, and snippets.

@mxaly
Last active September 6, 2015 12:38
Show Gist options
  • Save mxaly/10f5608db98f82b4d4f8 to your computer and use it in GitHub Desktop.
Save mxaly/10f5608db98f82b4d4f8 to your computer and use it in GitHub Desktop.

Docs

UJS helpers

  • registerComponent

    registerComponent(String componentName, class|function component)

    Register component so it's globally accessible.

    example:
    import MyComponent from 'my-component';
    
    registerComponent('MyComponentName', MyComponent);

    note: Registered components are accessible in globally exposed ReactRailsUJS under reactComponents property.

  • getComponent

    getComponent(String componentName)

    Shortcut for accessing registered component.

    example:
    getComponent('MyComponentName');
  • createComponent

    createComponent(String componentName[, Object props])

    Wrapper over React.createElement that creates ready to render component with props.

    example:
    createComponent('MyComponentName', {foo: 'bar'});
  • renderComponent

    renderComponent(String componentName, Object props, DOMElement container)

    Wrapper over React.render and React.createElement. Renders component with given props into specified DOM element.

    example:
    var element = document.getElementById('my-element');
    renderComponent('MyComponentName', {foo: 'bar'}, element);

Rails helpers

  • react_component

    react_component(String component_name[, Object props])

    Creates DOM node with props as data attributes in rendered view so ReactRailsUJS can grab it and mount proper component.

    example:
    <%= react_component('MyComponentName', MySerializer.new(my_data)) %>

    note: Object will parsed to JSON. Be careful when passing rails models as props - all its data accessible after .to_json will be exposed as data-attributes. We strongly recommend using serializers to control it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment