Skip to content

Instantly share code, notes, and snippets.

Always hacking somewhere

James Pearce jamesgpearce

Always hacking somewhere
View GitHub Profile
jamesgpearce /
Last active Sep 22, 2017
DIMOC: Do It Myself or Callback - a simple pattern for React components

TLDR: a React component should either manage its own state, or expose a callback so that its parent can. But never both.

Sometimes our first impulse is for a component to entirely manage its own state. Consider this simple theater seating picker that has a letter for a row, and a number for a seat. Clicking the buttons to increment each value is hardly the height of user-interface design, but never mind - that's how it works:

/* @flow */
var React = require('react');

var Letter: React.ReactClass = React.createClass({
 getInitialState: function(): any {