正在加载视频...

视频加载失败

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

301,101 次观看 • 2 年前 •via X (Twitter)

9 条评论

George Moller 的头像
George Moller2 年前

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 ↓

Daniel 的头像
Daniel2 年前

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

George Moller 的头像
George Moller2 年前

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

pavi2410 的头像
pavi24102 年前

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

George Moller 的头像
George Moller2 年前

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

anand tiwari 的头像
anand tiwari2 年前

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.

George Moller 的头像
George Moller2 年前

That's a valid approach as well Anand!

Irvin Zhan 的头像
Irvin Zhan2 年前

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

Piotr Monwid-Olechnowicz 的头像
Piotr Monwid-Olechnowicz2 年前

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>

相关视频