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

382
Views
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 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza 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!