I am having a little trouble calling a useEffect using a setstate that I passed as a prop to a child component. I am not sure this is the way to do this.
The code goes something like this:
const Parent = () => {
const [state, setState] = useState()
useEffect(() => {
console.log ('state updated');
}, [state])
return (
<>
<Child state={state} setState = {setState}/>
</>
)}
const Child = (props) => {
const update = () => {
props.setState("updated");
}
return(
<>
<button onClick={update}></button>
</>
)}
In this case, I expect the useEffect to run when the button is clicked but it does not. How do I fix it or is there a better way of doing this. Note that the two components are different files.
Thanks in advance!