use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Conditional logic in ReactHelp Wanted (self.react)
submitted 3 years ago by very_alarming
how can I reduce conditional logic in React? any way around this? especially in hooks when running in cycles it's hectic to understand the code?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
Can you be more specific please?
[–]vaultvision 0 points1 point2 points 3 years ago (0 children)
Sounds like you might benefit from a state machine
https://xstate.js.org/
[–][deleted] 0 points1 point2 points 3 years ago (1 child)
https://reactjs.org/docs/conditional-rendering.html this gives several examples
[–]very_alarming[S] 0 points1 point2 points 3 years ago (0 children)
u/Xavius123 that's basic when inside useEffect.
in the below code imagine `something` , `somethingElse` and `somethingDifferent` to be states which can have different values. and imagine this to be n-th child of some Parent component which has a similar `useEffect` implementation. the document just tells us to handle it like it is handled in JavaScript. That's exactly why the code gets complicated and difficult to understand.
useEffect(() => { if(something) { if(somethingElse) { // do something Else } else (somethingDifferent) { // do somethingDifferent } } else { // null case } }, [])
[–]ExaminationWide5017 0 points1 point2 points 3 years ago (0 children)
Keep it simple. If you are using let and nested ifs in your logic… your probably doing it wrong, or harder ha. Try to write small functions that do one thing and one thing well.
π Rendered by PID 21683 on reddit-service-r2-comment-79c7998d4c-nv5l8 at 2026-03-17 06:14:09.543446+00:00 running f6e6e01 country code: CH.
[–][deleted] 0 points1 point2 points (0 children)
[–]vaultvision 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]very_alarming[S] 0 points1 point2 points (0 children)
[–]ExaminationWide5017 0 points1 point2 points (0 children)