我正在尝试设置一个 onClick 事件来执行两个操作,第一个操作是更新子组件中的状态,然后更新父组件中的状态,请分享有关如何实现这一点的想法
I guess you need a callback state like this:
const [counter, setCounter] = useState(0);
const [parent, setParent] = useState(0);
const doSomething = () => {
setCounter(1);
}
useEffect(() => {
console.log('Do something after counter has changed', counter);
setPrent(2);
}, [counter]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.