Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save hopsoft/e4347ffd897f41be2bd7 to your computer and use it in GitHub Desktop.

Select an option

Save hopsoft/e4347ffd897f41be2bd7 to your computer and use it in GitHub Desktop.
React Component Structure

Guidelines

Each file should define a single (i.e. one) React component.

Dependencies

Immutable Data

Expose a single state attribute named data that is an immutable map. When state changes, rebuild the immutable map.

getInitialState: function () { 
  return { data: Immutable.Map({ ... }); }
}
(function (app) {
var components = (app.components || (app.components = {}));
app.components.ExampleComponent = React.createClass(
mixins: [],
propTypes: {...},
getInitialState: function () {
return {
data: Immutable.Map({ ... });
}
},
render: function () { ... },
// event handlers
onClick: function () { ... },
// custom component methods
data: function (key) {
return this.state.data.get(key);
},
doStuff: function () { ... }
});
}(window['www.example.com'] || (window['www.example.com'] = {})));
@hopsoft

hopsoft commented Jul 23, 2015

Copy link
Copy Markdown
Author

Good points. I'm struggling with finding the sweet spot of how much to break the app up into separate files.

On top of that, I've discovered a friendly way to use RequireJS with Rails. But now I need to decide how many pre-compiled assets to create vs individual JavaScript files to load as modules. Though having all the options is quite nice. https://medium.com/@hopsoft/requirejs-rails-73e7050cf173

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