Skip navigation
❌ Avoid difficult-to-read conditionals in React.
0:00 / 0:21

❌ Avoid difficult-to-read conditionals in React.

George Moller

@_georgemoller119K followers

Visit on X
1,130 likesDownloadOriginal post

185,805 views

❌ Avoid difficult-to-read conditionals in React. ✅ Instead you can use ts-pattern to express complex conditions in a single, compact expression.

11 Comments

Comments from the original post on X (Twitter)

  • George Moller's profile picture

    @_georgemoller

    Hey there 👋 Friendly reminder that I have more than 100 infographics and video animations just like this one. Check them out ↓

    11 likes

  • thgmhz's profile picture

    @thgmhz

    in some scenarios, you can avoid conditionals setting an object and getting the value by association:

    37 likes

  • Michael Lynch's profile picture

    @Michaelynch

    Personally, I wouldn't introduce a library for something that simple. Nothing wrong with a basic switch or if...else statement. Just don't use a nested ternary.

    27 likes

  • George Moller's profile picture

    @_georgemoller

    For most cases I use maps, for more complex pattern matching I use ts-pattern.

    5 likes

  • Marco Pfeiffer's profile picture

    @TheTrueNemo

    Just use If conditions. No library that everyone reading has to learn, no overhead, more readable than both examples. Maybe a bit longer, but if you have a lot of those conditions, then the approach is wrong anyways. A separate Map would be in order.

    8 likes

  • James Vanderzon's profile picture

    @jamesvanderzon

    just avoid React 🤪

    4 likes

  • George Moller's profile picture

    @_georgemoller

    😅 what library or framework do you use in the frontend? (If any)

    2 likes

  • Geoffrey Shepard's profile picture

    @GJShepard

    Nice explanation. I will say the hardest thing to read might be the "ss" font there in the word "address"

    2 likes

  • George Moller's profile picture

    @_georgemoller

    haha font ligatures right? I have mix feeling about them, sometimes I like them, sometimes I don't...

    1 like

  • Shripal Soni's profile picture

    @codewithshripal

    I didn't know about `ts-pattern` before. Thanks for sharing about it, George 🙌

    2 likes

  • George Moller's profile picture

    @_georgemoller

    It's a great solution for complex pattern matching for sure! try it out!