Загрузка видео...

Не удалось загрузить видео

На главную

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

185,781 просмотров • 2 лет назад •via X (Twitter)

Комментарии: 11

Фото профиля George Moller
George Moller2 лет назад

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

Фото профиля thgmhz
thgmhz2 лет назад

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

Фото профиля Michael Lynch
Michael Lynch2 лет назад

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.

Фото профиля George Moller
George Moller2 лет назад

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

Фото профиля Marco Pfeiffer
Marco Pfeiffer2 лет назад

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.

Фото профиля James Vanderzon
James Vanderzon2 лет назад

just avoid React 🤪

Фото профиля George Moller
George Moller2 лет назад

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

Фото профиля Geoffrey Shepard
Geoffrey Shepard2 лет назад

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

Фото профиля George Moller
George Moller2 лет назад

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

Фото профиля Shripal Soni
Shripal Soni2 лет назад

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

Фото профиля George Moller
George Moller2 лет назад

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

Похожие видео