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

238
Views
Is using an if statement to redirect user away if page does not exists good practice in React?

Basically, I am trying to route a user based on whether or not the page exists. So I tried to do an if statement to check the prop profileExists. If it is true, it would stay on the same page. Otherwise, it would reroute the user away to another route, which is the /homepage route. I was wondering if this is an ok thing to do in React or not. Please see code below:

renderWithRoot(component) {
  const { classes, profileExists, history } = this.props;

  if (profileExists === false) {
    history.push('/homepage');
  }

  return (
    <div className={classes.root}>
      {component}
    </div>
  );
}
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

It is ok, but not quite in the way you are using it. You should either issue the redirect as an imperative navigation in a useEffect hook

useEffect(() => {
  if (!profileExists) {
    history.replace('/homepage');
  }
}, [profileExists]);

...

return (
  <div className={classes.root}>
    {component}
  </div>
);

or issue the redirect as a declarative navigation via rendering the Redirect component.

if (!profileExists) {
  return <Redirect to='/homepage' />;
}

return (
  <div className={classes.root}>
    {component}
  </div>
);
about 4 years ago · Santiago Gelvez 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!