pass the the function to update the state of foo to another component as prop
Can I pass setFoo directly or should I write a function like fooHandler and pass it ?
function updateFoo(n) { setFoo(n); }
const Comp2 = ({updateFoo}) => { useEffect( ()=>{ updateFoo('new value for foo State from the child to the father') },[dependencies...] ) return <h1> I'm the children </h1> } const Comp1 = () => { const [foo,setFoo] = useState(0) return ( <Comp2 updateFoo={setFoo} /> ) } Hope the example can help
Обсуждают сегодня