【发布时间】:2020-08-03 16:01:35
【问题描述】:
我正在学习 React Hook 和 useEffect,但我可能没有掌握正确使用它的关键点。
我有以下场景:
- 如果用户切换下拉菜单,如果当前页面有任何更新,将检查是否有来自全局状态的
isAnyChange标志。 - 如果没有变化,将继续并重定向到下一页,
- 如果有任何更改,将显示确认对话框。
然后在确认对话框中,将有 2 个按钮:
- 4.1 要求保存、检查验证和重定向,
- 4.2 一请求不保存和重定向。
我使用不同的useEffect 包含了上述逻辑,但我不确定我是否正在做正确的方式,因为它看起来与我自己有关并且使事情变得复杂,什么是分解我的功能的最佳方式然后?我是否在这里通过使用那些local state flag 来触发不同的useEffect 进行正确的设计?不过我想不出更好的办法...
const pageComponent = props => {
const [isSelectChange, setIsSelectChange] = useState(false);
const [canRedirect, setCanRedirect] = useState(false);
const [isSaveCompleted, setIsSaveCompleted] = useState(false);
const anyChange = useSelector(state => return state.anyChange;);
const anyValidError = useSelector(state => return state.anyValidError;);
// step 1. dropdown change to change local state and trigger useEffect in step 2
const selectChange = () => {
setIsSelectChange(true);
}
// step 2. state isSelectChange to trigger step 3 howConfirmDialog or useEffect in step 6
useEffect(() => {
if (isSelectChange) {
if (anyChange) { showConfirmDialog(); }
else { setCanRedirect(); }
// should I placed it here? but this seems to trigger the same useEffect a second time even though no code is executing
// setIsSelectChange(false);
}
return () {
// clean up to reset the flag back to avoid looping here on isSelectChange, is it the right place to do it here?
setIsSelectChange(false);
}
}, [isSelectChange])
// step 3. dispatch a confirm dialog
const showConfirmDialog() => {
dispatch(openConfirmDialog(
dialog: <ConfirmDialog confirmhdlr={saveAndRedirect} cancelhdlr={cancelAndRedirect} />
);
}
// step 4.1 save and redirect
const saveAndRedirect = async () => {
await(savePage());
// this is to trigger another useEffect monitoring on isSaveCompleted flag
setIsSaveCompleted(true);
setCanRedirect(true);
}
// step 4.2 cancel and redirect
const cancelAndRedirect = () => {
setCanRedirect(true);
}
// step 5. state isSaveCompleted to trigger
useEffect(() => {
if (isSaveCompleted && !anyValidError) {
// this is to trigger another useEffect monitoring on "canRedirect"
setCanRedirect(true);
// should I placed it here? but this seems to trigger the same useEffect a second time even though no code is executing
// setIsSaveCompleted(false);
}
return () {
// clean up to reset the flag back to avoid looping here on isSaveCompleted flag, is it the right place to do it here?
setIsSaveCompleted(false);
}
}, [isSaveCompleted])
// step 6. state canRedirect to trigger redirect
useEffect(() => {
if (canRedirect) {
if (isSaveCompleted && !anyValidError) {
// this is to trigger another useEffect monitoring on "canRedirect"
setCanRedirect(true);
}
// should I placed it here? but this seems to trigger the same useEffect a second time even though no code is executing
// setCanRedirect(false);
}
return () {
// clean up to reset the flag back to avoid looping here on canRedirect flag, is it the right place to do it here?
setCanRedirect(false);
}
}, [canRedirect])
return (<select onChange={selectChange}>)
}
如你所见,我有不同的标志来触发不同的useEffect,我可以在一个函数中完成所有这些,那么为什么要在这里使用useEffect?
【问题讨论】:
-
您不需要在清理函数中重置状态 - 当组件卸载时调用它们,在这种情况下所有状态也会被清除。返回函数用于取消订阅等
-
@thedude,谢谢!那么我应该将重置状态放在 useEffect 执行中但在 return() 清理之外吗?似乎重置会再次调用useEffect,我不确定我在这里是否正确......
标签: javascript reactjs react-hooks