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

241
Views
React - responsive app - better to hide element in component via CSS or not render it

I'm currently working on a React.js-based app. Lets say we have a Header component in React with a small logo component inside it that should only be displayed at mobile resolution levels. I'm passing an isMobile prop from the parent component. This prop is based on:

const mql = global.matchMedia(`(min-width: 768px)`);
mql.addListener(() => this._mediaQueryChanged());
this.setState({
  mql: mql,
  isMobile: !mql.matches
});

_mediaQueryChanged() {
  this.setState({
    isMobile: !this.state.mql.matches
  });
}


<Header isMobile={this.state.isMobile} />

And in Header:

render() {
  const {isMobile} = this.props;
  const containerClass = classNames('header-component', {
    'is-mobile': isMobile
  });
  return (
    <header className={containerClass}>
      {
        isMobile && 
        (
          <section className="mobile-header">
            <Button className="toggle-menu" onClick={() => this._toggleMenu()}>
              <Icon name="menu" />
            </Button>
            <Logo className="header-logo" />
          </section>
        )
      }
      <span>login</span>
    </header>
  );
}

and if mql matches then it is not mobile. My question is: should I pass this param and re-render the component every time we change Media queries? During re-render hide/show logo component? Or just use CSS to show/hide it and the component will be mounted there all the time. No re-renders tho.

Thoughts?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I agree this may be a job for just CSS.

.header-class .logo {
  display:none;
}

@media only screen and (min-width: 768px) {
    .header-class .logo {
      display:block;
    }
}
over 4 years ago · Santiago Trujillo Report

0

I think it depends on what you're trying to achieve. If you have universal rendering with critical path CSS extraction and trying to shave milliseconds go for the react-only solution. Otherwise css-only solution will do but equally as well as the react-only solution because you're probably re-rendering already. That sneaky isMobile has just triggered a render somewhere else in your codebase (or will in the future); plus, you've just lost the extensibility of that react offers.

over 4 years ago · Santiago Trujillo 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!