Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
how to render if else and else if statement in react functional component
const GharwapasiForm = () => {

  const [activeStep,setActiveStep] = useState(0);
  const steps = [
    'Click Your Photo',
    'Personal Details',
    'Submit Application',
  ];

  
  return (
    <FormContainer className='row'>
        <Stepper activeStep={0} className="py-2" >
        {steps.map((label) => (
          <Step key={label}>
            <StepLabel>{label}</StepLabel>
            

          </Step>
        ))}
      </Stepper>
      

        
    </FormContainer>
  )
}

export default GharwapasiForm

Is active step one i want to render "step one"if step two i want to render "This is the step two" and similarly for the third step the problem is only know how to write if and else like {condition ? "render if":"render else"} what if i had to check more than one statement

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I think the clean way (not to compose ternary operators) is to create a component.

Example:

const StepTitle = ({ step }} => {
  if (step === 1) {
    return <span>step one</span>
  }
  if (step === 2) {
    return <span>This is the step two</span>
  }
  return <span>something</span>
} 

that will be for something more complex than just text, for text you can just:

const texts = { "1": "step one", "2": "This is the step two" }

// in code
return texts[step] || "some default"
about 4 years ago · Juan Pablo Isaza Relatório

0

You can do something like this.when state change your component getting rendering, so that you can write if condition inside the react component to check whether your activeStep state change.See the below code.

   const GharwapasiForm = () => {
    
      const [activeStep,setActiveStep] = useState(0);
      let activeStepString="";

      if(activeStep==0)
     {

        activeStepString="step one";
      }
   else if(activeStep==2){
     activeStepString="step two;

    }
  else if(activeStep==3){
    activeStepString="step three;

  }
else{
 activeStepString="step four;
}
      const steps = [
        'Click Your Photo',
        'Personal Details',
        'Submit Application',
      ];
    
      
      return (
        <FormContainer className='row'>
            <Stepper activeStep={0} className="py-2" >
            {steps.map((label) => (
              <Step key={label}>
                <StepLabel>{activeStepString}</StepLabel>
                
    
              </Step>
            ))}
          </Stepper>
          
    
            
        </FormContainer>
      )
    }
    
    export default GharwapasiForm
about 4 years ago · Juan Pablo Isaza Relatório

0

You can write code more like JSX.

const [activeStep, setActiveStep] = useState(0);

const titles = [
    <span className="one">one</span>,
    <h2 className="two">two</h2>,
    <div className="three">three</div>,
]

return (
    <div>
        {titles[activeStep]}
    </div>
);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda