'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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
