-
What is state?
-
What is
setState()
and what does it do?
- What happens when I call
setState()
?
- How do the values inside the
state
get rendered?
- Why does the lifecycle matter?
- Why do we have to use
className
instead ofclass
andbackgroundColor
instead ofbackground-color
?
Answers below
- What is state?
- An object
- that contains information that controls the output of render, i.e., state influences how does the UI look
- What is
setState()
and what does it do?
setState()
is a function that updates the state
- 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
- How do the values inside the
state
get rendered?
- The value gets passed into the JSX elements like so,
<div> { this.state.value } </div>
- Why does the lifecycle matter?
-
We want to control what happens when each component renders, updates, re-renders and disappears.
-
Lifecycle:
- Creation (birth of an element)
- Mounting (displaying of the element)
- Updating (change in the state of the element)
- Unmounting (removal of the element)
-
To learn more about the various lifecycle methods available, see here: https://programmingwithmosh.com/javascript/react-lifecycle-methods/
- Why do we have to use
className
instead ofclass
andbackgroundColor
instead ofbackground-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