简体   繁体   中英

React Hook useEffect has a missing dependency when passing props using React Hook

So i try to use Effect to passing the component from redux, here is my code:

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);

but it keeps geeting me warning like this in the console:

WARNING in [eslint]
src\pages\Login.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

Can someone explained to me where did i do wrong here....

Your effect hook depends on userList but it's not included in the dependency array. One useful alternative is to use the callback function form of the state setter:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM