Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
can't bring redux to react properly in react.js (and I think this is probably async problem)

I am developing a personal project and faced a problem. I couldn't solve this with my narrow-limited knowledge. So I'm asking your help now.

below is my redux state:

user
userData:{
_id(pin):"614141995694f8ab7357ae0d"
isAdmin(pin):false
isAuth(pin):true
email(pin):"wtf3@naver.com"
name(pin):"dsakljf"
role(pin):0}

and this is a useEffect that I would use axios:

useEffect(() => {
        axios.get('/api/blog/getBlogs')
        .then(response => {
          if(response.data.success){
            setBlogs(response.data.blogs)
          } else{
            alert("Couldn't get post list")
          }
        })
      })

I want to give user's id to axios so I can give user personal page later such as below:

 const user = useSelector(state=>state.user)

useEffect(() => {
        const userId={userId:user.userData._id}
        console.log(user.userData._id)
        axios.get('/api/blog/getBlogs',userId)
        .then(response => {
          if(response.data.success){
            setBlogs(response.data.blogs)
          } else{
            alert("Couldn't get post list")
          }
        })
      })

However, I got error instead of success and console said: 'can't read property '_id' of undefined'

I thought this might be a problem because I declared userId inside of useEffect So It tried to bring userId even before redux loaded. for that, I declared userId outside of useEffect. It seems works at the first moment, however, it returned the same error message as soon as I refresh the page. could you tell me how I can bring redux state in useEffect and use it properly? thx for reading, your help will be appreciated, If you need more detail of my code, don't hesitate to ask please.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!