Skip to content

Instantly share code, notes, and snippets.

@DevanB
Last active November 18, 2019 16:14
Show Gist options
  • Save DevanB/96b6bf9154d24ab3f97db817a76e7d9c to your computer and use it in GitHub Desktop.
Save DevanB/96b6bf9154d24ab3f97db817a76e7d9c to your computer and use it in GitHub Desktop.
React Training Orlando Notes

useState

A component has a stack, and adds hooks based like:

const Component = () => {
  const [value, setValue] = React.useState(5)
}
const stack = [
  { type: 'useState', value: 5, updater: fn }
]

This is why hooks need to be in the same order always (no conditionals).

const Component = () => {
  const [value, setValue] = React.useState(5)
  const [error, setError] = React.useState(null)
}
const stack = [
  { type: 'useState', value: 5, updater: fn },
  { type: 'useState', value: null, updater: fn },
]

By default React does a "shallow" compare to figure out if it needs to rerender.

If you give it objects, it will always rerender, because the objects are different.

Setting state is more or less "asynchronus". You might think of it as:

  1. set state
  2. console log state is still the "previous" state
  3. render happens
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment