React Hook useEffect在使用React Hook传递道具时缺少依赖项



所以我尝试使用效果来传递组件从redux,这是我的代码:

const initState = {
newAttribs: { ...props.state.Auth },
};
const [userList, setUserList] = useState(initState);
useEffect(() => {
setUserList({ ...userList, newAttribs: { ...props.state.Auth } });
}, [props.state.Auth]);
console.log("userList now", userList);

但是它总是在控制台中收到这样的警告:

WARNING in [eslint]
srcpagesLogin.jsx
Line 15:6:  React Hook useEffect has a missing dependency: 'userList'. Either include it or remove the dependency array. You can also do a functional update 'setUserList(u => ...)' if you only need 'userList' in the 'setUserList' call  react-hooks/exhaustive-deps

谁能给我解释一下我在哪里做错了....

你的效果钩子依赖于userList,但它不包括在依赖数组中。一个有用的替代方法是使用状态设置器的回调函数形式:

setUserList(userList => (
{ ...userList, newAttribs: { ...props.state.Auth }) }
);

相关内容

  • 没有找到相关文章

最新更新