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

141
Vistas
React Conditional Rendering Child

I'm new to React and trying to understand using props with conditional rendering. I have a component called Intro.js inside my app.js.

My goal is to click a button on a form in Intro.js and remove Intro from app.js I've been able to accomplish this with a button in app.js but not within Intro.js. How would I get this to work within the child component Intro.js

        const [introSection, setIntroSection] = useState(false)

        {
        introSection ? <Intro /> : true
        } 

        <Button onClick={() => setIntro(false)}> Remove Intro Section</Button>
            
       
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Firstly, your state update is also not aligned with the actual function. It should be setIntroSection instead of setIntro

<Button onClick={() => setIntroSection(false)}> Remove Intro Section</Button>

You're almost there with your logic. You just need to modify your logic like below

{
   introSection ? <Intro /> : null
}
<Button onClick={() => setIntroSection(false)}> Remove Intro Section</Button> 

But this is not a good way when we have a very complex structure in the true path, so I personally prefer this way

{introSection && <Intro />} //when it's false, nothing is rendered
<Button onClick={() => setIntroSection(false)}> Remove Intro Section</Button> 

If you want to do it inside Intro, you can pass a prop

<Intro isHidden={true} />

And in Intro, you can have this logic

const Intro = ({ isHidden }) => {
  if(isHidden) {
     return null
  }
  
  //true path to render your own component
  return <div></div>
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You should pass your setIntro/setIntroSection (it depends how you used it) function to Intro component as a prop, so then you will be able to access and use that state function. I hope this helps.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use setIntroSection, not setIntro.

A whole bunch of code will be helpful to answer.

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