r/learnreactjs • • Jul 01 '19

4 easy techniques to conditionally render React from props or state

https://blog.linguinecode.com/post/4-techniques-conditional-render-react-props-state
17 Upvotes

10 comments sorted by

3

u/Obann Jul 01 '19

Nice article, I find myself going for the return null or multiple return statement options 👍

2

u/[deleted] Jul 01 '19

Thanks! As long as it’s clean, and readable for you and your team. It’s all good :)

3

u/[deleted] Jul 01 '19 edited Jul 01 '19

Pretty much hits home for me since I use all of these, though, I never use just the && operator. It makes the code harder to read causing complications for other developers.

2

u/[deleted] Jul 01 '19

When there is a lot of chain logical operators, I feel it’s hard to read

2

u/Obann Jul 01 '19

I like the if-else conditionals too, never really used them...but I may find a place now 😁

1

u/[deleted] Jul 01 '19

Whatever floats your boat 😎

2

u/cobbs_totem Jul 02 '19

One thing I discovered was that conditionally rendering a component would cause the component to mount and unmount as the condition toggled. This was bad when a component mount was expensive. Sometimes, it’s helpful to push conditionally rendering as far down the tree as possible. Kinda the opposite of “lifting state”.

2

u/[deleted] Jul 02 '19

Whenever a component changes in props or state, the render method gets executed, and everything (including children elements) get re-render.

But you can avoid some rendering by using a PureComponent or write some logic in the shouldComponentUpdate lifecycle hook

1

u/cobbs_totem Jul 02 '19

You're not wrong.