Video wird geladen...

Video konnte nicht geladen werden

Zur Startseite

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

185,781 Aufrufe • vor 2 Jahren •via X (Twitter)

11 Kommentare

Profilbild von George Moller
George Mollervor 2 Jahren

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

Profilbild von thgmhz
thgmhzvor 2 Jahren

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

Profilbild von Michael Lynch
Michael Lynchvor 2 Jahren

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.

Profilbild von George Moller
George Mollervor 2 Jahren

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

Profilbild von Marco Pfeiffer
Marco Pfeiffervor 2 Jahren

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.

Profilbild von James Vanderzon
James Vanderzonvor 2 Jahren

just avoid React 🤪

Profilbild von George Moller
George Mollervor 2 Jahren

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

Profilbild von Geoffrey Shepard
Geoffrey Shepardvor 2 Jahren

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

Profilbild von George Moller
George Mollervor 2 Jahren

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

Profilbild von Shripal Soni
Shripal Sonivor 2 Jahren

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

Profilbild von George Moller
George Mollervor 2 Jahren

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

Ähnliche Videos