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

119
Visualizações
Piece of state changes in parent component but not in child components React

I have a component tree that looks as so:

<Profile>
   <FollowersAndFollowing>
      <Overlay>
         {children}
      </Overlay>
   </FollowersAndFollowing>
</Profile>

In <Profile/> I have a piece of state holding a boolean value:

const [showFollowers, setShowFollowers] = useState(false)

I am trying to prop funnel this piece of state to all of my components. In my <Profile/> I have these two functions.

  const handleShowFollowers = () => setShowFollowers(true)
  const handleHideFollowers = () => setShowFollowers(false)

  console.log('from profile', showFollowers) // logs true, then logs false

IN PROFILE

{showFollowers ? <FollowersAndFollowing showFollowers={showFollowers} handleHideFollowers={handleHideFollowers} /> : null}

FOLLOWERS AND FOLLOWING

const FollowersAndFollowing = ({ showFollowers, handleHideFollowers }) => {

  console.log('from followers', showFollowers) // logs true, then logs nothing at all

  return (
    <Overlay isShowing={showFollowers} currentTopPosition={0}>
      <h1>followers and following</h1>
      <button onClick={handleHideFollowers}>BACK</button>
    </Overlay>
  )
}

OVERLAY

const Overlay = ({ isShowing, children, currentTopPosition }) => {

  console.log('from overlay', isShowing) // logs true, then logs nothing at all

  useEffect(() => {
    if (isShowing) {
      document.body.style.overflow = "hidden";
    } else {
      document.body.style.overflow = "visible";
    }
  }, [isShowing])

  return (
    <div className={isShowing ? overlayStyles.showOverlay : overlayStyles.overlay} style={{ top: `${currentTopPosition}px` }}>
      {children}
    </div>
  )
}

When I trigger handleShowFollowers from my <Profile/> component I see showFollowers as true for all three components.

However when I trigger handleHideFollowers from the <FollowersAndFollowing/> component I see showFollowers flip to false in the parent component (profile) but not in any of the other two components. What could be causing this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This line is the problem:

{showFollowers ? <FollowersAndFollowing showFollowers={showFollowers} handleHideFollowers={handleHideFollowers} /> : null}

If showFollowers is false then your FollowersAndFollowing component won't render at all, that's why the console.logs do no log.

If you change it to just this:

<FollowersAndFollowing showFollowers={showFollowers} handleHideFollowers={handleHideFollowers} />

You can then handle hiding elements deeper down inside the children components and it should work fine.

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