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

91
Vistas
Hide Child Component Element After onClick using ReactJS

I'm new to React, learning by coding, here i have component A, which has select element with menuItems (all material ui), when user clicks select element and chooses from drop down, right after user has chosen whole component should go display:none, is this possible ? i mean user should not be able to see select element anymore on the page

English is not my mother language, so there might be mistakes.

suggestions/help is appreciated.

component A:

const A: React.FC<AProps> = (props) => {
  const handleChange = (e: React.ChangeEvent<{ value: unknown }>) => {
    const site = e.target.value as string;
    dispatch(changeActiveSite(site));
    if (site) {
      dispatch(getAnalysers(site));
    } else {
      dispatch(clearSiteData(site));
    }
  };

  const sites = [
    {
      ident: "",
      name: "None",
    },
  ].concat(sitess);


  return (
    <React.Fragment>
      <FormControl className={classes.formControl}>
        <InputLabel id="site-select-input-label">site</InputLabel>
        <Select
          id="site-select"
          value={currentSiteId}
          labelId="site-select-input-label"
          onChange={(e) => handleChange(e)}
        >
          {sites.map((site) => {
            return (
              <MenuItem key={site.ident} value={site.ident}>
                {site.name}
              </MenuItem>
            );
          })}
        </Select>
      </FormControl>
    </React.Fragment>
  );
};

that component is in component B like this: <div > <A site={site} /> </div>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Let's imagine your MenuItem.js, specifically, its render() and constructor(). You'll want it to be able to be hidden/not-displayed, or visible/displayed. Use a state attribute for hidden to control this, your render will probably look like...

constructor(props) {
    super(props);
    this.state = {
        'hidden':false,
    };
}

render () {
    if(this.state.hidden) {
        return '';
    }
    
    return (
        <div
            onClick={(e) => this.handleOnClick(e)}
        >
            {this.props.value}
        </div>
    );
}

Notice I also added a handleOnClick(e) handler up above! That will simply call this.setState({'hidden':true}), so like...

handleOnClick(e) {
    this.setState({'hidden':true});
}

I have an answer to a similar question elsewhere, if it might also help: How to set one component's state from another component in React

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