Skip to content

Instantly share code, notes, and snippets.

@ronsuez
Created April 16, 2017 02:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ronsuez/b986b6c7852a0c1cefd6cf04ae46b0e9 to your computer and use it in GitHub Desktop.
Save ronsuez/b986b6c7852a0c1cefd6cf04ae46b0e9 to your computer and use it in GitHub Desktop.
function shouldIkeepSomethingInReactState() {
if (canIcalculateItFromProps()) {
// Don't duplicate data from props in state.
// Calculate what you can in render() method.
return false;
}
if (!amUsingItInRenderMethod()) {
// Don't keep something in the state
// if you don't use it for rendering.
// for example, API subscriptions are
// better off as custom private fields
// or variables in external modules.
return false;
}
// You can use React state from this!
return true;
// taken from: https://twitter.com/dan_abramov/status/749710501916139520
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment