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

385
Visualizações
Is it a good practice to use switch case in useEffect Reactjs?

I am handling some functionality in some functions. Basically I am handling some states in multiple functions. Those functions need to be called when particular state updates. Now there are a lot of states to be updated. So I tried this approach, I created a function for specific case and then set their state according to that value but it is all in a useEffect.

useEffect(() => {
    if (notificationAction && actualCase) {
        switch (actualCase) {
            case firstCase:
                handleStatesBasedOnFirstCase();
                break;
            case firstCase:
                handleStatesBasedOnSecondCase();
                break;
        }
    }
}, [actualCase, notificationAction])

And that function is also a callback.

const handleStatesBasedOnFirstCase = useCallback(() => {
    if (someState !== props.someState) {
        // Setting some states here
    }
}, [somestates...])

Is it a good practice? Or shall I just give conditions while initialising the state.

const [someState, setSomeState] = useState(props.someState || props.someOtherValue);

I think setting props in useState is not a good practise. That is the reason why I am trying to maintain multiple functions to set a proper state as per the proper cases and conditions. Please help me.

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

0

As per @Nikki9696 answer in the comments, using reducer will do the job.

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