1

The variable userInfo is correctly shown in the console, but can't be set for the state.

It works the second time so I assume the state is being set before data is correctly retrieved.

const [userData, setUserData] = useState({});

try {
  const response = await axios.post("/usuarios/oauth-twitch", { data: code });
  const userInfo = await response.data.user_data;
  console.log(userInfo);
  setUserData(userInfo);
} catch (error) {
  console.log(error);
}
Ahmed Sbai
  • 10,695
  • 9
  • 19
  • 38
Lucian
  • 21
  • 4
  • what is `code` ? – Ahmed Sbai Aug 05 '23 at 17:37
  • "code" is the token that I'm using to fetch data from the Twitch API That is working fine as "userInfo" appears fine in the console. It just doesn't get set on the state, but it works the second time that the try catch function is fired – Lucian Aug 05 '23 at 17:42
  • In fact the state should be updated but the issue must be the moment when you are logging it, have a look [here](https://stackoverflow.com/a/75844926/13488990) it might help – Ahmed Sbai Aug 05 '23 at 17:46

0 Answers0