Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda