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

106
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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