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

110
Visualizações
React Component not rendering a passed props

I'm trying to pass an array of users as a props to a component, when I change something and click save, the array is showed in the component but when I hit refresh, the array is disappeared, here is my code:

First in my App.js I'm reading an array of users from the database (works perfectly shows the list of users) :

const [users,setUsers] = React.useState([]);
React.useEffect( () => {
      async function fetchData() {
        await axios.get('/api/users/')
        .then(response => {
         setUsers(response.data);
      });
      }
        fetchData();
      }
    , []);

Then, also in App.js, I'm rendering a ListComponent that takes the users array and shows the users:

return (
     <ListComponent users={users} />
);
}

In my ListComponent after a page refresh the console.log shows an empty array []

const ListComponent = (props) => {
React.useEffect(()=>{
      console.log(props.users); // []
},[])
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you refresh the page, the ListComponent will be remounted, and what you are logging, is the state of the component just after it is mounted, so the user array is not already fetched. If you want log it when it is fetched, you should add the user array in the dependency array of the useEffect function:

const ListComponent = (props) => {
  React.useEffect(()=>{
    console.log(props.users); // Should be an empty array first, then updated if your fetch function is working properly
  },[props.users]);
  // ...
};

If you still cannot see the user array, it means that something is not happening as expected in your fetchData function I guess.

about 4 years ago · Juan Pablo Isaza Relatório
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