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

331
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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