Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda