0

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....

1 Answer 1

2

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 }) } ); 
Sign up to request clarification or add additional context in comments.

3 Comments

can you explained it more...
it works but it seems it dont get it..
You use userList inside the effect so you should make sure to include it in the dependency array to make sure the effect runs with its current value. The function form of the setter gets rid of this dependency since the function is defined inside the effect body (and will always refer to the current version of state).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.