How to notify parent component of property change when using react hooks?
useEffect
takes a second argument which denotes when to execute the effect. You can pass in the state value to it so that it executes when state updates. Also you can have multiple useEffect
hooks in your code
const [propA, setPropA] = useState(props.propA);const [propB, setPropB] = useState(props.propB);useEffect(() => { props.onChangePropA(propA); }, [propA]);useEffect(() => { props.onChangePropB(propB); }, [propB]);<div> <button onClick={e => {setPropA(e.target.value)}}>Prop A</button> <button onClick={e => {setPropB(e.target.value)}}>Prop B</button></div>