Skip to Main Content
React Design Patterns and Best Practices - Second Edition
book

React Design Patterns and Best Practices - Second Edition

by Carlos Santana Roldán
March 2019
Intermediate to advanced content levelIntermediate to advanced
350 pages
7h 28m
English
Packt Publishing
Content preview from React Design Patterns and Best Practices - Second Edition

Conditionals

Things get more interesting when we start working with conditionals, for example, if we want to render some components only when certain conditions are matched. The fact that we can use JavaScript in our conditions is a big plus, but there are many different ways to express conditions in JSX, and it is important to understand the benefits and problems of each one of these to write code that is both readable and maintainable.

Suppose we want to show a logout button only if the user is currently logged into our application.

A simple snippet to start with is as follows:

  let button;    if (isLoggedIn) {     button = <LogoutButton />;   }     return <div>{button}</div>;

This works, but it is not very readable, especially if there are multiple ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

React 17 Design Patterns and Best Practices - Third Edition

React 17 Design Patterns and Best Practices - Third Edition

Carlos Santana Roldán

Publisher Resources

ISBN: 9781789530179Supplemental Content