Ada*_*ley 0 reactjs react-router
我正在尝试实现一些需要监听位置的东西。
useEffect(() => {
props.history.listen(location => {
// do stuff here
});
return () => {
props.history.listen(location => {
// now stop doing stuff
});
};
}, []);
Run Code Online (Sandbox Code Playgroud)
我已经看到了参考,unlisten()但它没有得到很多赞成票,如果我这样做了,console.log(props.history.unlisten())它会作为not a function
这是在 App.js 中完成的。我假设应该清理它,但我不确定它是否真的需要清理。
需要清理吗?如果是这样,正确的做法是什么?
props.history.listen 的返回值是一个函数。调用该函数将进行拆卸,因此您只需要返回它:
useEffect(() => {
const unlisten = props.history.listen(location => {
// do stuff here
});
return unlisten;
}, []);
Run Code Online (Sandbox Code Playgroud)
或者:
useEffect(() => {
return props.history.listen(location => {
// do stuff here
});
}, []);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
646 次 |
| 最近记录: |