2022-06-16
4183
#react
Esteban Herrera
337
Jun 16, 2022 â‹… 14 min read

React conditional rendering: 9 methods with examples

Esteban Herrera Family man. Java and JavaScript developer. Swift and VR/AR hobbyist. Like books, movies, and still trying many things. Find me at eherrera.net

Recent posts:

D3 Js Adoption Guide: Overview, Examples, And Alternatives

D3.js adoption guide: Overview, examples, and alternatives

D3.js is a powerful JavaScript library for creating tailored data visualizations. Let’s see why you should use D3.js in your next project.

Eze Sunday
Jan 19, 2024 â‹… 10 min read
Using Semantic-Release To Automate Releases And Changelogs

Using semantic-release to automate releases and changelogs

Demonstrate how to automate releases and release notes with semantic-release in GitLab.

Sebastian Weber
Jan 18, 2024 â‹… 10 min read
React Native Push Notifications A Complete How To Guide

React Native push notifications: A complete how-to guide

Learn all about how to implement push notifications in React Native in this ultimate guide and step-by-step tutorial.

Fortune Ikechi
Jan 17, 2024 â‹… 12 min read
Exploring Effect A Meta State Rxjs Like Framework For Writing Typescript

Exploring Effect, a meta-state RxJS-like framework

The Effect library helps you better handle async code, types in async scenarios, and errors in TypeScript projects. Let’s see how.

Isaac Okoro
Jan 17, 2024 â‹… 6 min read
View all posts

4 Replies to "React conditional rendering: 9 methods with examples"

  1. Nice article!

    Why do you still use class components? It’s 2020, function components with hooks are not an “alternative” way. They are THE way to go and classes are unnecessary for the examples you show.

    Your article is a great resource for beginner React developers, but also confusing, because you use class components.

    1. This post was originally published several years ago, before the stable release of the Hooks API, and we just updated it a few months back. We’ve added an editor’s note to clarify. Thanks for keeping us honest.

  2. Althought this article has inmense value and all of this is valid React, when an application gets big, using live vanilla javascript to condition the render adds complexity and you start building an enviroment very prone to errors later, good practice will be create a component that handles the condition taking it as a prop and returns the children or null, and reuse it across the app, making your render entirely declarative instead of imperative… has been an old trade in San Francisco since the begining of React.. truth is you can call it how ever you want,, but make sure the component do that.. back in the pre-hooks days ppl use to do it using a HOC ….

Leave a Reply