Skip to content

Instantly share code, notes, and snippets.

@CheshireSwift
Last active August 23, 2019 16:00
Show Gist options
  • Save CheshireSwift/46779f463a5998cc497029dfac2a3fa7 to your computer and use it in GitHub Desktop.
Save CheshireSwift/46779f463a5998cc497029dfac2a3fa7 to your computer and use it in GitHub Desktop.
React Data Storage

Variables/modules/props/state/context

Scope Needs to trigger rerender on change Mutability You should use...
Local No/never changes Any Local constants/variables
Local Yes (Locally) Immutable Props
Local Yes Mutable State
Global No/never changes Any Constants/variables in a module, imported where required
Global Yes Mutable Context

The table has a theoretical gap in the "global, should trigger rerender, immutable" row, but if it's globally immutable then it'll never change, and thus rerendering is moot (i.e. it can just live in a module).

Local/global aren't very strict here. They loosely mean "relevant to this component + a small neighborhood" and "relevant to many unrelated pieces of code" respectively.

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