I have a functional React component that roughly looks like this:
const MyComponent = (prop1) => {
const [myState, setState] = useState(prop1)
return <>
{myState && <div>Some text...</div>}
<div onClick={() => setState(true)}>Click me</div>
</>
}
This obviously doesn't work because React only computes the initial state part once, so myState
isn't updated on prop1
change.
Is there a way to make this work without using useReducer
? Is this a good use case for useEffect
?