Загрузка видео...
Не удалось загрузить видео
❌ Avoid difficult-to-read conditionals to render elements in react. ✅ Instead use utility components for conditional rendering.
301,101 просмотров • 2 лет назад •via X (Twitter)
Комментарии: 9

I've been working professionally with React for more than 8 years and I compiled all my knowledge into 100+ infographics just like this one. Check them out ↓

Why go through all that when you can simply have a helper function with a normal switch case that returns the copy?

I like this pattern because its a more React-ish way of conditionally render elements.

I think this is cleaner and much readable this way. No new library is introduced.

I like this approach too! Either this or the switch component for me. Ternary expressions in jsx are a mess.

From readability perspective looking nice and clean. 👌 As we already have switch case in Js that can used inside a function and i prefer using that. Instead of creating switch functionality using react components & props.

That's a valid approach as well Anand!

You lose some Typescript type safety here. Plus this feels like unnecessary layers of abstraction. Just use a variable.

This is very bad advice in a pretty video. Branches in ternaries aren't eagerly evaluated, and children are. This crashes ⬇️ with "cannot read properties of null". <Case condition={data == null}>Loading...</Case> <Case condition={data != null}>{data.text}</Case>
