Video wird geladen...

Video konnte nicht geladen werden

Zur Startseite

❌ Avoid difficult-to-read conditionals to render elements in react. ✅ Instead use utility components for conditional rendering.

301,101 Aufrufe • vor 2 Jahren •via X (Twitter)

9 Kommentare

Profilbild von George Moller
George Mollervor 2 Jahren

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 ↓

Profilbild von Daniel
Danielvor 2 Jahren

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

Profilbild von George Moller
George Mollervor 2 Jahren

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

Profilbild von pavi2410
pavi2410vor 2 Jahren

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

Profilbild von George Moller
George Mollervor 2 Jahren

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

Profilbild von anand tiwari
anand tiwarivor 2 Jahren

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.

Profilbild von George Moller
George Mollervor 2 Jahren

That's a valid approach as well Anand!

Profilbild von Irvin Zhan
Irvin Zhanvor 2 Jahren

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

Profilbild von Piotr Monwid-Olechnowicz
Piotr Monwid-Olechnowiczvor 2 Jahren

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>

Ähnliche Videos