Answer the question
In order to leave comments, you need to log in
How to get rid of double data passing through useContext?
There is App.js in which I pass the state to the component through useContext. Further in the component in useEffect I use this state BUT!! comes undefined first, then the state changes to the desired one. The problem is that useEffect'e is requesting data to the server and the 2nd load is obtained.
app.js
<UserContext.Provider value={{ user, setUser }}>
<Header />
</UserContext.Provider>
const { user, setUser } = useContext(UserContext)
useEffect(() => {
const token = user.token
const getResouce = async () => {
const getRes = await axios
.get('/api/game/res', {
headers: { 'x-auth-token': token },
})
.catch((error) => {
console.log(error.response)
})
}
}, [user])
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question