koo*_*koo 31 reactjs react-hooks
让我解释一下此代码的结果,以便轻松地询问我的问题。
const ForExample = () => {
const [name, setName] = useState('');
const [username, setUsername] = useState('');
useEffect(() => {
console.log('effect');
console.log({
name,
username
});
return () => {
console.log('cleaned up');
console.log({
name,
username
});
};
}, [username]);
const handleName = e => {
const { value } = e.target;
setName(value);
};
const handleUsername = e => {
const { value } = e.target;
setUsername(value);
};
return (
<div>
<div>
<input value={name} onChange={handleName} />
<input value={username} onChange={handleUsername} />
</div>
<div>
<div>
<span>{name}</span>
</div>
<div>
<span>{username}</span>
</div>
</div>
</div>
);
};
Run Code Online (Sandbox Code Playgroud)
当ForExample component坐骑,“效果”将被记录。这与有关componentDidMount()。
每当我更改名称输入时,都会记录“效果”和“清理”。反之亦然,自从我添加[username]到的第二个参数以来,每次更改用户名输入都不会记录任何消息useEffect()。这与componentDidUpdate()
最后,在ForExample component卸载时,将记录“清理”。这与有关componentWillUnmount()。
我们都知道。
总之,只要重新渲染组件(包括卸载),就会调用“清理”
如果要使该组件仅在卸载时记录“清理”,则只需将第二个参数更改useEffect()为[]。
但是,如果我更改[username]为[],ForExample component将不再实现componentDidUpdate()for名称输入。
我要做的是,使组件componentDidUpdate()仅支持名称输入和componentWillUnmount()。(仅在卸载组件时记录“清理”)
Meh*_*iee 43
您可以使用多种用途
例如,如果我的变量是data1,我可以在我的组件中使用所有这些
useEffect( () => console.log("mount"), [] );
useEffect( () => console.log("will update data1"), [ data1 ] );
useEffect( () => console.log("will update any") );
useEffect( () => () => console.log("will update data1 or unmount"), [ data1 ] );
useEffect( () => () => console.log("unmount"), [] );
Run Code Online (Sandbox Code Playgroud)
Tho*_*lle 26
由于清理操作不依赖于username,因此您可以将清理操作放在一个单独的对象中useEffect,该对象将空数组作为第二个参数。
例
const { useState, useEffect } = React;
const ForExample = () => {
const [name, setName] = useState("");
const [username, setUsername] = useState("");
useEffect(
() => {
console.log("effect");
},
[username]
);
useEffect(() => {
return () => {
console.log("cleaned up");
};
}, []);
const handleName = e => {
const { value } = e.target;
setName(value);
};
const handleUsername = e => {
const { value } = e.target;
setUsername(value);
};
return (
<div>
<div>
<input value={name} onChange={handleName} />
<input value={username} onChange={handleUsername} />
</div>
<div>
<div>
<span>{name}</span>
</div>
<div>
<span>{username}</span>
</div>
</div>
</div>
);
};
function App() {
const [shouldRender, setShouldRender] = useState(true);
useEffect(() => {
setTimeout(() => {
setShouldRender(false);
}, 5000);
}, []);
return shouldRender ? <ForExample /> : null;
}
ReactDOM.render(<App />, document.getElementById("root"));Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>Run Code Online (Sandbox Code Playgroud)
Man*_*mar 14
您可以简单地将其写为:
useEffect(() => {
return () => {};
}, []);
Run Code Online (Sandbox Code Playgroud)
为了添加到已接受的答案中,我遇到了类似的问题,并使用类似的方法和下面的人为示例解决了它。在这种情况下,我需要记录一些参数,componentWillUnmount并且如原始问题中所述,我不希望每次更改参数时都记录。
const componentWillUnmount = useRef(false)
// This is componentWillUnmount
useEffect(() => {
return () => {
componentWillUnmount.current = true
}
}, [])
useEffect(() => {
return () => {
// This line only evaluates to true after the componentWillUnmount happens
if (componentWillUnmount.current) {
console.log(params)
}
}
}, [params]) // This dependency guarantees that when the componentWillUnmount fires it will log the latest params
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16605 次 |
| 最近记录: |