Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

87
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!