无法在卸载时重置值。在挂载上,它可以setValue {a:2}。但是在卸载组件时,setValue没有重置值,
const Child = ({setValue}) => {
useEffect(() => {
setValue({a:2})
//clean up on unmount
return () => {
console.log('cleanup');
setValue({});
};
}, []);
}
在父母中
const Parent = () => {
const [value, setValue ] = useState({})
console.log(value) // returns {a:2} even after unmounting child
}
组件卸载和清理,也称为console.log(“清理”),但setValue不能重置。
发布于 2022-03-17 10:50:32
你的代码应该能用。你怎么给孩子送道具?
https://stackoverflow.com/questions/71510898
复制相似问题