Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Is it a good practice to use switch case in useEffect Reactjs?

$
0
0

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.


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>