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

93
Views
Elegant ways of handling conditional rendering of multiple components

Say I have a wizard-like view with an arbitrary number of steps:

const StepsComponent = () => {
  const [stage, setStage] = useState(1);
  const stageProps = {stage, setStage};

  const stageMachine = () => {
    switch (stage) {
      case 1:
        return <One {...stageProps} />;
      case 2:
        return <Two {...stageProps} />;
      case 3:
        return <Three {...stageProps} />;
      default:
        return <One {...stageProps} />;
    }
  };

  return (
    <>
      {stageMachine()}
    </>
  );
}

Are there more elegant ways of handling such cases, other than switch statements or ternary expressions?
If I would have wizards with 10+ steps, then it'd be a real mess to manage it.

Probably I could do something like this but this seems hacky, doesn't it?

const stageMachine = Object.freeze({
  1: <One {...stageProps} />,
  2: <Two {...stageProps} />,
  3: <Three {...stageProps} />
});

Also I don't like the idea of invoking stageMachine function in return, it is considered a bad practice?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can try this

const StepsComponent = () => {
  const [stage, setStage] = useState(1);

  return (
    <>
      {stage === 1 && <One />}
      {stage === 2 && <Thow />}
      {stage === 3 && <Three />}
    </>
  );
}

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!