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

330
Vistas
react.js React.UseEffect only rendered once upon page load but not when prop changes

I am trying to update my component everytime the prop changes:

export default function ChatMessages(props) {
  const [chatRooms, setChatRooms] = React.useState([]);
  React.useEffect(() => {
    (async () => {
      var x = await GetMessages(props.statePassDown);
      console.log(x);
      setChatRooms(x);
    })();
  }, []);

  return (
    <div class="mainChatView">
      <div>{props.statePassDown}</div>
      {chatRooms.map((chatRoom, index) => {
        return (
          <ul>
            <li key={index}>
              <table>
                <tr>
                  <td>
                    chatID: {chatRoom.chatID}
                    <br></br>
                    message: {chatRoom.message}
                  </td>
                </tr>
              </table>
            </li>
          </ul>
        );
      })}
    </div>
  );
}

But unfortunatley, this code is only ever called once. I can see that the prop changes through this div:

<div>{props.statePassDown}</div>

This line rerenderes everytime a button is pressed inside another component, but the whole mapping function isnt.

How can I alter my code that it responds to the change in props.statePassDown?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You're explicitly setting the effect to have no dependencies:

React.useEffect(()=>{
  // ...
}, []);  // <-- empty dependencies

That means it will only ever be executed on component mount. (If you don't pass any dependency array, then the effect is executed after each component update.)

Change the dependencies to have the effect be executed when those values change.

React.useEffect(()=>{
  // ...
}, [props.statePassDown]);
about 4 years ago · Juan Pablo Isaza Denunciar

0

export default function ChatMessages(props) {

const [chatRooms, setChatRooms] = React.useState([]);
React.useEffect(()=>{
  (async () => {
      var x = await GetMessages(props.statePassDown)
      console.log(x)
      setChatRooms(x)
  })()
},[props.statePassDown])  // pass what you are trying to watch change 
...
// ... here in the dependency array
about 4 years ago · Juan Pablo Isaza Denunciar

0

Also, a good thing to note about the useEffect hook is that the return statement inside of a useEffect hook will trigger on the component did unmount lifecycle. ex.

useEffect(() => {
  // ... on component mount

  return () => {
    // ... component did unmount 
  }
}, [/* dependency array */]);
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