Home > Enterprise >  React - JS not storing object in global
React - JS not storing object in global

Time:12-16

I am trying to insert an object (user) into a global so i can refer back to it without having to do numerous api-calls when needed.

The data is being grabbed from the service fine, however it is not getting stored

The code:


  useEffect(()=>{
    // this needs to be here because the user doesnt exist on first render
    if (user){
      fetch (`${window.ipAddress.ip}/User/getByEmail`,{
        method: "POST",  
        headers:{'Content-Type':'application/json'},
        body: JSON.stringify({ email: user.email }) 
      })
      .then(res=>res.json())
      .catch(error =>{ 
        setErrorVal(true);
        console.log("error: "   error);
      })
      .then((result)=>{
          console.log(result) // this prints appropriately
          window.BackendUser = { result } // this isnt being set for some reason
          setExecutedFetch(true);
      })
    }
},[isAuthenticated])

CodePudding user response:

You can not write brackets around your result, bracketing is JSX syntax.

window.BackendUser = result 
  • Related