-
The
state
of a React class is a special property that controls the rendering of a page. When you change the state, React knows that the component is out-of-date and will automatically re-render. When a component re-renders, it modifies the rendered output to include the most up-to-date information instate
. For class-based components we store it locally. -
React documentation encourages developers to adopt React Hooks to manage state with functional components when writing new code, rather than using class-based components.
-
It returns a variable with the current state value (not necessarily the initial state) and another function to update this value.
-
How do we store state in class-based components?
State objects initial values are stored in this.state and this.setState is use to update the states. -
How do we store state in functional components?
The useState() hook is used to store the initial state. -
What is the return value of a useState call?
It returns the current state variable.
- How do we store state in class-based components?
We use constructor and declare it by using
this.state = { name: this.props.name }
- How do we store state in functional components?
We use hooks
const [state, setState] = useState();
- What is the return value of a useState call?
useState()
returns two values. One is the current state and second value is a function to update this state.
- How do we store state in class-based components?
We store state in class inside constructor as an object value using {}
this.state = {
brand: “Ford”
}
and update with this.setState
- How do we store state in functional components?
We store state in functional component using useState hook.
const [state, setState] = useState(‘initial state value’)
Initial state value could be single string or an array of object.
- What is the return value of a useState call?
We pass the initial state to the useState function and it returns a variable
with the current state value and a function to update the state.
- We have to instanciate it in the constructor.
- Declaring them using useState(name).
3.It returns the current state and a function that can update it.
- By using this.state and this.setState
- We use hooks to store the state
- Returns the current state after being updated
-
declare the state in the constructor() and change the values by this.setState()
-
use the State hook as useState()
-
returns a pair of values: the current state and a function that updates it
1.) in an object this.state.
To call it use this.setState
2.) we use a hook like useState()
3.) the current state and a function to update it
- We use
this.state = {
name: "Tony"
};
this.setName = this.setName.bind(this);
- We use
const [name, setName] = useState("Tony");
3.It returns the value that we passed to the useState hook and a function to change that state.
- How do we store state in class-based components?
this.state = {
name: "Paul"
};
this.setName = this.setName.bind(this);
- How do we store state in functional components?
const [name, setName] = useState(“Peter”);
- What is the return value of a useState call?
useState is a hook. It rerenders the state when called.
1:
Using a constructor and this.setState()
2:
Using props and “useState”
3:
The current state
-
How do we store state in class-based components?
Inside the class constructor, we attribute the object containing the state variables to the this.state
variable -
How do we store state in functional components?
By importing and using the useState() hook -
What is the return value of a useState call?
useState returns both a variable with the current state value, and the function to update that value
- setState() function & declare the state variables in the class.
- useState()
- the current state
How do we store state in class-based components?
with this.state and this.setState
How do we store state in functional components?
using the useState hook
What is the return value of a useState call?
it returns the current state
- How do we store state in class-based components?
State is stored as properties in the class variable this.state
(can have multiple)
The initial state can be set in the constructor by directly setting this.state
Subsequent updates to state is done via the function this.setState()
-
How do we store state in functional components?
By declaring state variables with
useState(name)
-
What is the return value of a useState call?
useState()
returns two variables the first is reference to the value (a getter?) and second a setter function for updating the value. need separate calls to
useState` for each propertyuseState call returns the current state.
- How do we store state in class-based components?
this.state() - How do we store state in functional components?
useState() - What is the return value of a useState call?
The return value of a useState call includes a pair of values which are the currentState and a function that updates the currentState.
How do we store state in class-based components?
this.setState in constructor
How do we store state in functional components?
useState() hook
What is the return value of a useState call?
Returns current state after update
1.) How do we store state in class-based components?
We store it by adding it in to the constructor as a property followed by binding it and then using event handler to allow the user to set the new value.
2.) How do we store state in functional components?
We can use the useState for storing variables.
3.) What is the return value of a useState call?
The most recent state that was stored
- How do we store state in class-based components?
{ key:“value” } - How do we store state in functional components?
useState() - What is the return value of a useState call?
the current state
- Store class state to “this.state” directly in the constructor, and if you want to make any changes, refer to it with “this.setState()”
2.Store functional component states with “useState” and change with “setVariable”
- Return value of a “useState” call is a variable with the current state value, as well as a function to update this value