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

90
Visualizações
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 Respostas
Responde à pergunta

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 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