Update string based on switch case

Viewed 53

I am creating a multistep form in React which uses a switch case to render a component based on its ID:

App.js

function App() {
  const steps = [
    { id: 'location' },
    { id: 'questions' },
    { id: 'appointment' },
    { id: 'inputData' },
    { id: 'summary' },
  ];

  return (
    <div className="container">
      <ApptHeader steps={steps} />
      <Step steps={steps} />
    </div>
  );
}

Steps.js


const Step = ({ steps }) => {
  const { step, navigation } = useStep({ initialStep: 0, steps });
  const { id } = step;
  const props = {
    navigation,
  };
  console.log('StepSummary', steps);
  switch (id) {
    case 'location':
      return <StepLocation {...props} steps={steps} />;
    case 'questions':
      return <StepQuestions {...props} />;
    case 'appointment':
      return <StepDate {...props} />;
    case 'inputData':
      return <StepUserInputData {...props} />;
    case 'summary':
      return <StepSummary {...props} />;
    default:
      return null;
  }
};

In my <ApptHeader /> component in App.js, I want to change the Title and subtitle of the string in the header based on the component rendered in the switch case.

const SectionTitle = ({ step }) => {
  console.log('step', step);

  return (
    <div className="sectionWrapper">
      <div className="titleWrapper">
        <div className="title">Title</div>
        <div className="nextStep">
           SubTitle
        </div>
      </div>
      <ProgressBar styles={{ height: 50 }} />
    </div>
  );
};
export default SectionTitle;

How can I accomplish this? I feel like I might be writing redundant code if I have to make a switch case again for each title/subtitle. Thanks in advance.

1 Answers

You can use a pattern like this

const steps = {
    location: { id: 'location', title: 'Location', component: StepLocation },
    questions: { id: 'questions', title: 'Questions', component: StepQuestions },
    appointment: { id: 'appointment', title: 'Appointment', component: StepDate },
    inputData: { id : 'inputData', title: 'InputData', component: StepUserInputData },
    summary: { id: 'summary', title: 'Summary', component: StepSummary },
};

Then while using it inside your Steps.js will become

const Step = ({ steps }) => {
    const { step, navigation } = useStep({ initialStep: 0, steps });
    const { id } = step;
    const props = { navigation };

    const Component = steps[id].component; 
    return <Component {...props} steps={steps} />;
};

Your SectionTitle.js will become like this

const SectionTitle = ({ step }) => {
    console.log('step', step);

    return (
        <div className="sectionWrapper">
            <div className="titleWrapper">
                <div className="title">Title</div>
                <div className="nextStep">{step.title}</div>
            </div>
            <ProgressBar styles={{ height: 50 }} />
        </div>
    );
};

export default SectionTitle;

This way you can avoid code redundancy.

Be Sure to update your other parts of code like useStep to accept and Object instead of and Array

Related