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

233
Vistas
Hiding and showing content on click using React

In a nutshell, i am creating a case study for a potential job, the employer wants me to use a React app to create it...

I want to create a button that has the start function that:

  1. Hides original content

  2. displays the hidden content,

i got the hidden content to show but the original content is still visible, any help?

my code below:

import React, { useState } from 'react'

function Body() {

    const [show, setShow] = useState(true);
    const [hide, setHidden] = useState(true);

  return (
      
    <>  
        <div className='container'>
            <div className="start-container">
                <h2>Click Start To View The Application</h2>
                <button onClick={ () => setShow(s => ! s) } className='btn'>Start!</button>         
            </div>
            
            {/* URL Link Input */}
            <div>
                {!show ? <form action="GET">
                    <input type="text" />
                    <button className='btn'>Submit</button>
                </form> : null }
                
            </div>
        </div>
    </>
    
  )
}

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

0

You are close, you need to have the original content in the ternary so it's hidden once you toggle show. I also changed setShow to set show to false rather than the previous value since it doesn't matter because the button is not toggable because once you click it and can't re toggle the original content.

import React, { useState } from 'react'

function Body() {    
    const [show, setShow] = useState(true);

    return (
        <div className='container'>
            {show ? (
               <div className="start-container">
                  <h2>Click Start To View The Application</h2>
                  <button onClick={() => setShow(false)} className='btn'>Start!</button>         
               </div>
            ) : (
               <form action="GET">
                  <input type="text" />
                  <button className='btn'>Submit</button>
               </form>
            )
        </div>
  )
}

export default Body
about 4 years ago · Juan Pablo Isaza Denunciar

0

it dose not need hide state and you can toggle visibility just with show state. try this:

 { show ? <form action="GET">
  <input type="text" />
  <button className='btn'>Submit</button>
    </form> : null
 }
about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use an appStarted state and then render your component conditionnally:

const { useState } = React

function Body() {
  const [appStarted, setAppStarted] = useState(false)

  return (
    <div className="container">
      {appStarted ? (
        <div>
          <h2>Hidden Content</h2>
        </div>
      ) : (
        <div className="start-container">
          <h2>Click Start To View The Application</h2>
          <button onClick={ () => setAppStarted(true) } className='btn'>Start!</button>
        </div>
      )}
    </div>
  )
}


ReactDOM.render(<Body />, document.getElementById("root"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="root"></div>

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