Following is a simple React Toggle functionality. But I have a confusion. In the first click, I understand that the 'setState' function makes the 'state' to 'true' so the content shows. But in the second click 'setState' is already true. Then how the false state working.
- First Click on the button
- 'changeState' function makes 'state' to 'true'
- 'state' is 'true' so the content is shows
- Trigger the second click
- What will happend ?
Can any one explain it in simple way. Thanks in advance!
import React, {useState} from 'react'
function App() {
const [state,setState]=useState(false);
console.log(state);
const changeState=()=>{
setState(!state);
}
return (
<>
{state ? <div>Show Content</div> : null}
<button onClick={changeState}>Toggle</button>
</>
)
}
export default App