Skip to content

Instantly share code, notes, and snippets.

@yi-mei-wang
Created October 11, 2019 02:47
Show Gist options
  • Save yi-mei-wang/5814c4bef5b19a06fdaf42fafff8fb85 to your computer and use it in GitHub Desktop.
Save yi-mei-wang/5814c4bef5b19a06fdaf42fafff8fb85 to your computer and use it in GitHub Desktop.
Check my React understanding - Day 1

Do I understand...

  1. What is state?

  2. What is setState() and what does it do?

  1. What happens when I call setState()?
  1. How do the values inside the state get rendered?
  1. Why does the lifecycle matter?
  1. Why do we have to use className instead of class and backgroundColor instead of background-color?

Answers below


Do I understand...

  1. What is state?
  • An object
  • that contains information that controls the output of render, i.e., state influences how does the UI look
  1. What is setState() and what does it do?
  • setState() is a function that updates the state
  1. What happens when I call setState()?
  • render() gets called, which results in a change in UI if there is a difference between the current and previous state
  1. How do the values inside the state get rendered?
  • The value gets passed into the JSX elements like so,
    <div> { this.state.value } </div>
  1. Why does the lifecycle matter?
  • We want to control what happens when each component renders, updates, re-renders and disappears.

  • Lifecycle:

    1. Creation (birth of an element)
    2. Mounting (displaying of the element)
    3. Updating (change in the state of the element)
    4. Unmounting (removal of the element)
  • To learn more about the various lifecycle methods available, see here: https://programmingwithmosh.com/javascript/react-lifecycle-methods/

  1. Why do we have to use className instead of class and backgroundColor instead of background-color?
  • Because JSX is still Javascript, so we need to use camelCase
  • HOWEVER, our components should use PascalCase, i.e., the first letter of each word should start be capitalised
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment