Skip to content

Instantly share code, notes, and snippets.

@amirfefer
Last active May 13, 2018 14:26
Show Gist options
  • Save amirfefer/a4c669eecdd41f5609d6d724a4e8b704 to your computer and use it in GitHub Desktop.
Save amirfefer/a4c669eecdd41f5609d6d724a4e8b704 to your computer and use it in GitHub Desktop.
How to add a react component to foreman

Folder structre

Create a folder under /webpack/assets/javascripts/react_app/components/ with the following structure (filenames should be upper CamelCase):

webpack/assets/javascripts/react_app/components/<Compoment-name>
├── <Compoment-name>Actions.js
├── <Compoment-name>Reducer.js
├── <Compoment-name>.fixtures.js 
├── <Compoment-name>.scss.js // if needed
├── <Compoment-name>.js
├── index.js // the redux connected file 
└── __tests__
    ├── <Compoment-name>Actions.test.js
    ├── <Compoment-name>Reducer.test.js
    ├── <Compoment-name>.test.js
    ├── integration.test.js
    └── __snapshots__
        ├── All snapshot files (created automatlicy by `npm test -- -u`)

Register

in /webpack/assets/javascripts/react_app/components/componentRegistry.js file Import the new component:

import <Component-Name> from '<component-path>';

under coreComponets array, add this item:

  { name: '<Compoment-registered-name>', type: <component-name> },

Mount

Create a mounter in a specific erb.html file

<%= mount_react_component(`<Compoment-registered-name>`, `<elemnt id which wrapps the component>`, <component data>.to_json) %> 

where the component data goes to component's props as data prop (this.props.data)

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