Medium
What will this component display?
const MyComponent=()=>{
const[index, setIndex]= useState(1);
setIndex(2);
return<div>{index}</div>;
}
Author: Vincent CotroStatus: PublishedQuestion passed 3413 times
Edit
5
Community Evaluations
Auteur anonyme
13/11/2023
Because updating your state will create a new render. You should only update your state conditionnaly (for exemple using a "onClick" or a condition)
The loop will look like this :
- Rendering
- Updating state (with setIndex(2))
- Rendering
- Updating state (with setIndex(2))
etc...
Similar QuestionsMore questions about React