I'm making a website using React, Nodejs, Expressjs and Mysql. It has a login, register and profile pages. When I logged in I'm redirecting users with useNavigate to the profile page. Profile page shows the user email.
But when I navigate users to Profile, in the first display, Profile page is not showing user email. I have to refresh to page to see email.
I'm using useContext and wrapped all application with Provider. There's no problem. But when i navigate to Profile, useContext working a little bit late and it's not changing state instantly. Is there a way to solve this?
Top comments (8)
@ IDURAR , we use react context api for all UI parts , and we keep our data layer inside redux .
Here Article about : 🚀 Mastering Advanced Complex React useContext with useReducer ⭐ (Redux like Style) ⭐ : dev.to/idurar/mastering-advanced-c...
Its hard to give help with no codesandbox.io example. Have you checked that you update the state of the user before you do the redirect?
I'm using backend too. So, i don't know if i can use Codesandbox to show my issue. I'll share some codes to indicate my issue
This is my Login page. I'm using "useContext". I created a useEffect to see if user already logged in. authState.status is checking if there's a cookie in the browser in the backend.
When I click "Login button". loginUser() function is working. This function is creating new user. If there's not an error, it changes the state and creating a session. After that its navigate to profile
This is my App.js. In useEffect, I'm making a get request to /login to checking if there's a cookie in the browser in the backend. app.post and app.get are totally different things in the backend
this is app.get
I'm sorry if it is too much ask :(
My issue is. When i go to Profile in the first time. I have to refresh the page to see the email from the context
In you App.js you aren't setting the same auth state as you do in you Login component. It seems that you have missunderstood the concept of context a bit. Or there are some important bits of code missing here. You need to create a "AuthProvider" that stores your context. And you can handle the login and the "check auth cookie" request in that Provider instead of doing it in different parts of the app.
I can't really point you in the right correction based on this. I recommend reading kentcdodds.com/blog/authentication... to get some understanding of how it can be done.
Is your
app.get("/login", ...)
function only returning what got passed inreq.session.user
or have you stripped out some logic there?Also this useEffect needs to have authState.status in its dependency array like this:
otherwise it will only check that status on initial render. You can read about it here reactjs.org/docs/hooks-reference.h...
Yess. I already have an AuthContext.Provider in App.js and I imported it
app.get is just returning req.session.user. I created it in app.post('/login'). Here it is
Thank you for the tips and the sources. I will check immediately
hello please i'm new in react.js and i work on a website who allow to listen audio.but when i play one audio all audios begin to play i want.
how solve this issue thank you.
Hello. I never work with audios. Sorry. But you can create a post that has this tags. #help #react
Thank you for your answer