Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
Why won't the authState update? [ReactJS]
Author Summary
OpenForPeople is in ReactJS
Post Body

I'm working on the authorization context for my project and somehow the authState won't update.

I expectat that when I log the authState in the async function I get the updated state but it simply doesn't. I tried wrapping it into aseperate function and call it inside the async function and even the useEffect but it makes no difference.

When I log the data from the API request I do get the requested information, I can log them like they are and I get the information I want. The problem is just in updating the state.

The async function is wrapped in a useEffect.

This is the state hook

const [authState, setAuthState] = useState({
user: {
username: '',
mail: '',id: ''
},
authStatus: 'pending'
})
const token = localStorage.getItem('token')

This is the useEffect

useEffect(() => {
const currentTime = new Date().getTime().valueOf() / 1000const
decodedToken = jwtDecode(token)

if (token) {
if (decodedToken.exp > currentTime) {
console.log(currentTime)
getUser()}
else {
setAuthState({
...authState,
user: '',
status: 'done'
})}}
}, [])

This is the async function

async function getUser() {

try {const {data} = await axios.get('url',

{headers: {"Content-Type": "application/json","Authorization": Bearer ${token},}})

setAuthState({...authState,

user:{username: data.username,mail: data.email,id: data.id},authStatus: 'done'})console.log(data)console.log(authState)}catch (e){console.error(e)}}

* Note that I did not include the actual url because it seemed like the right thing to do. The request itself works perfect.

Author
Account Strength
100%
Account Age
3 years
Verified Email
Yes
Verified Flair
No
Total Karma
20,361
Link Karma
45
Comment Karma
20,233
Profile updated: 4 days ago
Posts updated: 1 year ago

Subreddit

Post Details

Location
We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago