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

198
Visualizações
Why is React useeffect not updated when props are destructured

I have had some issues with my props not updating correctly when they are destructorized in combination with useEffect dependencies. I now know how I can fix the issue but I do not understand why it happens.

The example below does not work properly and does not show changes when the props changes.

const Devices = ({ apis }) => {
  const [ availableApis, _setAvailableApis ] = useState([])
  const availableApisRef = useRef( availableApis )
  const setAvailableApis = v => {
    availableApisRef.current = v
    _setAvailableApis( v )
  }

  useEffect(() => {
    let availableApis = []

    apis.forEach( v => {
      console.log(v)
      if( v.available === true )
      {
        availableApis.push( v )
      }
    })

    console.log(availableApis)

    setAvailableApis( availableApis )
  }, [ apis ])

  return <somecomponent key={v.name} />
}

If I replace const Devices = ({ apis }) => { with const Devices = props => {
and replace }, [ apis ]) with }, [ props ]) the changes are rendered.

I thought this would have the same effect, but it does not.

Note: This problem does not occur when useEffect effect is not used, but the variable is simply used in a component.

const ApiStatus = ({ available, apiName, openLogin }) => {
  return (
    <StatusContainer onClick={openLogin}  available={available}>
      <p> { apiName } - { available ? "Beschikbaar" : "Uitgelogd" } </p>
    </StatusContainer>
  )
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If I replace const Devices = ({ apis }) => { with const Devices = props => { and replace }, [ apis ]) with }, [ props ]) the changes are rendered.

Those are very different things. The first re-runs the effect when apis changes, ignoring any other changes. The second re-runs the effect any time props, the object, changes. (And I think you get a new object [though possibly with the same contents] on every render of the child component, so you probably don't want that.)

If you accept props rather than {apis}, the equivalent dependency array for useEffect is [props.apis].

Looking at the code of your effect, it looks like it depends on apis and not on other props, so it makes sense for the dependency array to be [apis] (or [props.apis] if you don't destructure).

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