Lists, Fragments, and Conditional Rendering
You have learned a lot about React so far. You now know why we need props, what state is and how it differs from props. You have learned how to implement a React component as well as to differentiate between a React component and a React element. You also learned how to use JSX to accurately depict the tree of elements that is being rendered and how you can leverage lifecycle methods to react to changes in your data. All of these lay the groundwork for simple React applications.
But there are a few details that we have not yet examined (or not explained in detail). However, these details will become more relevant the more complex our applications get.
Lists (as in working with data arrays), so-called Refs (which are references to the DOM representation of a React element), Fragments (a special component that does not leave traces or elements in the output) and Conditional Rendering based on props and state are part of these details and will be examined in this section.
They are too important to not mention them in the Basics section of the book but each topic on its own does not quite warrant its own chapter. Let's look at them now.

Lists

Lists refer to plain JavaScript arrays — in this instance: simple data that can be iterated over. They are essential for React, even plain JavaScript development, and it is hard to imagine developing without them. ES2015+ offers many nice declarative methods such as Array.map(), Array.filter() or Array.find() that can even be used as expressions in JSX (if surrounded by curly brackets {}).
I have mentioned expressions in JSX and how to use them already in the chapter on JSX. But let's recap briefly: Arrays can be used as an expression in JavaScript and therefore also be included in JSX. If surrounded by curly brackets, they will be treated as child nodes during the transpilation step.
But there's more: Array.map() enables us to not only work with data but it can return modified items which in turn can also contain JSX. This adds further flexibility and allows us to transform data sets into React elements.
Let's assume that we want to show list of cryptocurrencies. The array containing the data has the following form:
1
const cryptos = [
2
{
3
id: 1,
4
name: 'Bitcoin',
5
symbol: 'BTC',
6
quotes: { EUR: { price: 7179.92084586 } },
7
},
8
{
9
id: 2,
10
name: 'Ethereum',
11
symbol: 'ETH',
12
quotes: { EUR: { price: 595.218568203 } },
13
},
14
{
15
id: 3,
16
name: 'Litecoin',
17
symbol: 'LTC',
18
quotes: { EUR: { price: 117.690716234 } },
19
},
20
];
Copied!
First, we will aim to show this data as a simple unordered list in HTML. A resulting component could look similar to this:
1
const CryptoList = ({ currencies }) => (
2
<ul>
3
{currencies.map((currency) => (
4
<li>
5
<h1>
6
{currency.name} ({currency.symbol})
7
</h1>
8
<p>{currency.quotes.EUR.price.toFixed(2)}</p>
9
</li>
10
))}
11
</ul>
12
);
Copied!
The component could then be used like this:
1
<CryptoList currencies={cryptos} />
Copied!
The result is an unordered list with list items containing the crypto currency and their price. However, we also encounter an error in the console:
React expects a key prop for all of the values returned by arrays or iterators. The primary reasoning behind this rule is to make it easier for React's Reconciler (the React Comparison algorithm) to identify and compare list elements. The Reconciler can spot whether an array element was added, removed or modified if the key prop has been given. This prop needs to be a unique identifier which only appears once in the array. Normally, the id of a data set is used for this.
In our example, we can take the id of each item. Amended, the example now looks like this:
1
const CryptoList = ({ currencies }) => (
2
<ul>
3
{currencies.map((currency) => (
4
<li key={currency.id}>
5
<h1>
6
{currency.name} ({currency.symbol})
7
</h1>
8
<p>{currency.quotes.EUR.price.toFixed(2)}</p>
9
</li>
10
))}
11
</ul>
12
);
Copied!
The key only has to be unique within the iterator compared to its sibling elements but not inside the component. We can easily use the CryptoList component elsewhere using the same key - even within the same component. Just don't use the key again in the same loop.
If your data set does not contain an easily distinguishable id, the index of the array can be used as a last resort. This is not recommended though and can lead to unexpected behavior in the rendering of user interfaces and also impact performance.
It is important that the key prop is always present in the top-level component or array element within the iterator but not in the JSX returned by the component.
To illustrate this point further, I'm going to transform the above list item into its own CryptoListItem component.
1
const CryptoListItem = ({ name, symbol, quotes }) => (
2
<li>
3
<h1>
4
{name} ({symbol})
5
</h1>
6
<p>{quotes.EUR.price.toFixed(2)}</p>
7
</li>
8
);
Copied!
What do we notice? The key prop which we added beforehand is no longer present. Our map() call would now look like this:
1
const CryptoList = ({ currencies }) => (
2
<ul>
3
{currencies.map((currency) => (
4
<CryptoListItem
5
key={currency.id}
6
name={currency.name}
7
symbol={currency.symbol}
8
quotes={currency.quotes}
9
/>
10
))}
11
</ul>
12
);
Copied!
Although we actually render a <li></li> element under the hood, the <CryptoListItem /> needs to contain the key prop as it is the top level component within our Array.map() call in this JSX snippet.
A little bit off-topic: We could even simplify the CryptoList component by using Object spread syntax:
1
const CryptoList = ({ currencies }) => (
2
<ul>
3
{currencies.map((currency) => (
4
<CryptoListItem key={currency.id} {...currency} />
5
))}
6
</ul>
7
);
Copied!
This way, all properties of the currency objects are passed as props to the CryptoListItem component.
If we didn't use an iterator such as Array.map(), a list would need to be explicitly defined like this:
1
const MyList = () => (
2
<ul>
3
{[<li key="1">One</li>, <li key="2">Two</li>, <li key="3">Three</li>]}
4
</ul>
5
);
Copied!

Fragments

Fragments are some sort of a special component and allow us to create valid JSX without leaving visible traces in the rendered markup. They are a solution to the "problem" of only ever returning a single element at the top in JSX. This is valid JSX:
1
render() {
2
return (
3
<ul>
4
<li>Bullet Point 1</li>
5
<li>Bullet Point 2</li>
6
<li>Bullet Point 3</li>
7
</ul>
8
);
9
}
Copied!
But this isn't:
1
render() {
2
return (
3
<li>Bullet Point 1</li>
4
<li>Bullet Point 2</li>
5
<li>Bullet Point 3</li>
6
);
7
}
Copied!
In this example, multiple elements are being returned in the render() method without a surrounding element, leading to an error. We do not always want to create new elements though, especially if the surrounding element is found in a parent component and the child element is found in its own component.
On top of that, many elements (such as table, ul, ol, dl, ...) do not allow for div elements to be used as an intermediary wrapper (ignoring the fact that we would also litter the markup by using divs). As we are only permitted to ever return a single root element from a component, Fragments can be incredibly useful. We could transform our example from above into the following:
1
render() {
2
return (
3
<React.Fragment>
4
<li>Bullet Point 1</li>
5
<li>Bullet Point 2</li>
6
<li>Bullet Point 3</li>
7
</React.Fragment>
8
);
9
}
Copied!
The rule that every element returned by a loop needs to have a key prop, still holds. Using a Fragment this is still possible. Let's illustrate this further by examining another slightly more complex yet more realistic example:
1
import React from 'react';
2
import ReactDOM from 'react-dom';
3
4
const TicketMeta = ({ metaData }) => (
5
<dl>
6
{Object.entries(metaData).map(([property, value]) => (
7
<React.Fragment key={property}>
8
<dt>{property}</dt>
9
<dd>{value}</dd>
10
</React.Fragment>
11
))}
12
</dl>
13
);
14
15
ReactDOM.render(
16
<TicketMeta
17
metaData={{
18
createdAt: '2018-06-09',
19
author: 'Manuel Bieh',
20
category: 'General',
21
}}
22
/>,
23
document.getElementById('root')
24
);
Copied!
The resulting output would be the following:
1
<dl>
2
<dt>createdAt</dt>
3
<dd>2018-06-09</dd>
4
<dt>author</dt>
5
<dd>Manuel Bieh</dd>
6
<dt>category</dt>
7
<dd>General</dd>
8
</dl>
Copied!
It would simply not be possible to wrap a div or span or another element around the <dt></dt> or <dd></dd>. Trying to do that would result in this:
1
<dl>
2
<div>
3
<dt>createdAt</dt>
4
<dd>2018-06-09</dd>
5
</div>
6
<div>
7
<dt>author</dt>
8
<dd>Manuel Bieh</dd>
9
</div>
10
<div>
11
<dt>category</dt>
12
<dd>General</dd>
13
</div>
14
</dl>
Copied!
... and is invalid HTML! A dl element only permits dt and dd as its child element. The Fragment helps us to alleviate these situations and creates valid JSX without creating invalid markup. It was only introduced in React 16.3 and meant that some React components were unnecessarily complex to deal with this problem to avoid violating JSX or HTML rules.
Fragment components can also be imported directly from React using a named import:
1
import React, { Fragment } from 'react';
Copied!
Now, the notation of <Fragment> instead of <React.Fragment> can be used saving us multiple key strokes.
Using Babel 7 for transpilation, we can shorten the notation even further. An empty element can created using:
1
<>Fragment in shorthand syntax</>
Copied!
This is a neat and tidy method to reduce the amount of fragments we need to explicitly define by name. But be careful: using the shorthand is not possible in loops as empty elements cannot contain any props. Elements in a loop require us to define a key prop thus forcing us to use <React.Fragment> instead.

Conditional rendering

Rendering components based on different conditions, conditional rendering for short, is a central concept in React. As React components are composed of JavaScript functions, objects and classes under the hood, conditions behave just as they would in regular JavaScript.
A React component renders a state of a user interface based on its props and its current state. In an ideal situation, this means that the component is free from side effects. To correctly deal with these different and changing parameters, using the render function to react to different conditions is a powerful feature. If my parameter is A, render this; if my parameter is B, render that. If I have incoming data in a list, show this data as an HTML list. If I do not have any data, show a placeholder instead.
If this sounds relatively simple, I can assure you, it is. But one should still be aware of knowing how to do conditional rendering in JSX. The render() function of Class components as well as Functional components can return a React element (also in form of JSX), a string, a number, null (if nothing should be rendered) or an array of these types.
There are a few methods of keeping the render() method nice and clean which I will explain now.

if/else

Probably the most simple and commonly known way to conditionally render is using if/else:
1
const NotificationList = ({ items }) => {
2
if (items.length) {
3
return (
4
<ul>
5
{items.map((notification) => (
6
<li>{notification.title}</li>
7
))}
8
</ul>
9
);
10
}
11
return <p>No new notifications</p>;
12
};
Copied!
This is a relatively simple use case. The NotificationList component receives a list of items in the form of props. If the list contains any entries at all, they are rendered as a list item of an unordered list. If however the list is empty, a message is returned informing the user that no new notifications are available.
Another more complex example: let's imagine we are working with a value that we want to make editable. Our component differentiates between the different modes of edit and view. Depending on which mode we are currently in, we either want to simply show text (View mode) or be able to see a text field containing the previously entered value (edit mode).
1
import React from 'react';
2
import { render } from 'react-dom';
3
4
class EditableText extends React.Component {
5
state = {
6
value: null,
7
};
8
9
static getDerivedStateFromProps(nextProps, prevState) {
10
if (prevState.value === null) {
11
return {
12
value: nextProps.initialValue || '',
13
};
14
}
15
return null;
16
}
17
18
handleChange = (e) => {
19
const { value } = e.target;
20
this.setState(() => ({
21
value,
22
}));
23
};
24
25
setMode = (mode) => () => {
26
this.setState(() => ({
27
mode,
28
}));
29
};
30
31
render() {
32
if (this.state.mode === 'edit') {
33
return (
34
<div>
35
<input
36
type="text"
37
value={this.state.value}
38
onChange={this.handleChange}
39
/>
40
<br />
41
<button onClick={this.setMode('view')}>Done</button>
42
</div>
43
);
44
}
45
46
return (
47
<div>
48
{this.state.value}
49
<br />
50
<button onClick={this.setMode('edit')}>Edit</button>
51
</div>
52
);
53
}
54
}
55
56
render(
57
<EditableText initialValue="Example" />,
58
document.getElementById('root')
59
);
Copied!
The relevant part can be found in the render() method of the component. The state is tested against its property value in mode. If edit is the current value in state, we directly return the input field with an "early return". If this is not the case, we assume that the "standard case" is taking place meaning that the current mode is view. The else part of this condition is not actually necessary here and would only add unnecessary complexity. Both times, the text is rendered with the difference that it is an editable value of an input field in one case, and a simple text node in the other. A button is included to switch between the different modes of view and edit.
Such if, if/else or if/else if/else constructs are common if you want to create output based on state and props within a component. I will explain them in more detail in just a moment.

null

No, this is not a mistake. Returning null is the most simple case of conditional rendering. If the render() method of a component returns null, nothing is rendered and also does not appear in the DOM. This can be useful for displaying error components that should only be displayed if the error has actually occurred.
1
render() {
2
if (!this.state.error) {
3
return null;
4
}
5
6
return (
7
<div className="error-message">{this.state.error.message}</div>
8
);
9
}
Copied!
Using this form of a conditional check, we test whether the state contains an error property. If this is not the case, null is being returned. Otherwise, an error message containing the error in state is returned in a div. We achieve this by using a simple if condition similar to what we have used above.

Ternary Operator

The conditions that we have just seen are often used to deal with relatively big changes in a component. In many situations, we only want to differentiate between minor differences, for example setting a CSS class if a certain state is set. The ternary operator helps us to do just that. Let's refresh our knowledge. A ternary operator takes the following form: condition ? met : not met. For example: isLoggedIn ? 'Logout' : 'Login';
That's our first example for a ternary operator in JSX! It can be used within props but also in conditions to differentiate between different forms of output to render based on conditions. To make the example more concrete, we could use the info from above to include it in a button:
1
render() {
2
const { isLoggedIn } = this.props;
3
return (
4
<button type="submit">{ isLoggedIn ? 'Logout' : 'Login' }</button>
5
);
6
}
Copied!
We will always return a button, but based on its isLoggedIn prop it can either include the message: Logout or Login.
Equally, the ternary operator can be used in props. Let's assume that we want to render a list of users in which some users have been deactivated. In this case, we want to be able to set a class to mark it using CSS. Markup that deals with this problem could look like this:
1
render() {
2
const { user } = this.props;
3
return (
4
<div className={user.isDisabled ? 'is-disabled' : 'is-active'}>{user.name}</div>
5
);
6
}
Copied!
Users that have been deactivated are now marked with the is-disabled class, whereas active users are denoted with is-active.
Even complex JSX can be displayed using the ternary operator. As long as you follow the rule to use parentheses if the JSX spans multiple lines:
1
render() {
2
const { country } = this.props;
3
return (
4
<div>
5
<p>State:</p>
6
{country === 'de' ? (
7
<select name="state">
8
<option value="bw">Baden-Württemberg</option>
9
<option value="by">Bayern</option>
10
<option value="be">Berlin</option>
11
<option value="bb">Brandenburg</option>
12
[]
13
</select>
14
) : (
15
<input type="text" name="state" />
16
)}
17
</div>
18
);
19
}
Copied!
In this example, a select list containing all German states is rendered if we previously selected de (for Germany) as our country. In all other cases a simple text input is shown to user in which they can enter their state freely. However, careful consideration should be given when to use the ternary operator: it can become a little hard to read quickly if complex JSX is used.

Logical AND (&&) and Logical OR (||)

The logical operator seems to resemble the ternary operator at first glance, but it is even shorter and more precise. As opposed to the ternary operator, a second "else" case is not needed. If the condition of the logical AND operator is not met, the expression simply returns undefined resulting in no visible markup for the user interface:
1
render() {
2
const { isMenuVisible } = this.props;
3
return (
4
<header>
5
{ isMenuVisible && <Menu /> }
6
</header>
7
);
8
}
Copied!
In this example, we test against the value of the isMenuVisible prop and check if it is true. If that is the case, it will return the Menu component. If the result is false, undefined is returned and nothing else will be rendered to the screen.
In combination with the logical OR operator, we can emulate the behavior of the ternary operator:
1
render() {
2
const { isLoggedIn } = this.props;
3
return (
4
<button type="submit">{ isLoggedIn && 'Logout' || 'Login' }</button>
5
);
6
}
Copied!
The button will be labelled Logout if the isLoggedIn prop is true, or Login if the user is logged out.

Custom render() methods

Another way to increase the readability during complex conditional rendering is to move parts from the regular render() method to separate renderXY() methods. The regular render() method still forms the core of the component and decides which parts of the user interface to show to the user. Thus, this method should not become overly complex or contain any unnecessary logic.
It is not uncommon to move parts of long and complex render() methods into much smaller, more digestible chunks and implement these as custom class methods. If proper naming is used, this technique usually aids readability and understanding. Often these custom render() blocks are combined with if blocks:
1
class Countdown extends React.Component {
2
renderTimeLeft() {
3
// […]
4
}
5
6
renderTimePassed() {
7
// […]
8
}
9
10
render() {
11
const { currentDate, eventDate } = this.props;
12
if (currentDate < eventDate) {
13
// currentDate is before eventDate so render countdown
14
return this.renderTimeLeft();
15
}
16
// time is over so render how much time has passed since then
17
return this.renderTimePassed();
18
}
19
}
Copied!
This can improve readability of the render() method but also increases the complexity of the component slightly. Many people recommend to move parts of the code into their own Function components instead though (myself included).
As soon as you begin to consider moving parts of your code into custom render() methods within your component, you should think about moving these into their own separate Function components.

Custom Components with complex conditions

Instead of using multiple render() methods, we can create new Function components. These will receive props from their parent component and then take care of displaying their own data as an independent, self-governing and testable component.
Careful consideration should be given as to what data actually needs passed down to the new child component(s), and how to pass this data, as the component(s) should not contain too much logic or state.
Using custom components becomes useful once the render() method in one component has become too complex or if the same elements are used repetitively within a component.
Let's look at a form which consists of many similar text fields. Each of these text fields is embraced by its own paragraph, and contains a label and a type attribute. The label also needs to be equipped with an id that is unique to each field that it represents.
1
render() {
2
return (
3
<form>
4
<p>
5
<label for="email">
6
Email
7
</label>
8
<br />
9
<input type="email" name="email" id="email" />
10
</p>
11
<p>
12
<label for="password">
13
Password
14
</label>
15
<br />
16
<input type="password" name="password" id="password" />
17
</p>
18
<input type="submit" value="Send" />
19
</form>
20
);
21
}
Copied!
We only have two form fields in this example but in most projects you will find many more fields in way more complex forms. Even in this case, it might make sense to extract recurring fields into their own components to save ourselves keystrokes.
First, create a TextField component and then cut the recurring JSX from the form component into this one:
1
const TextField = ({ id, label, ...HTMLInputAttributes }) => (
2
<p>
3
<label for={id}>{label}</label>
4
<br />
5
<input {...HTMLInputAttributes} id={id} />
6
</p>
7
);
8
9
export default TextField;
Copied!
This new component receives an id which is needed to link the label to the text input. Using Object Rest/Spread, we add all remaining props to the input element as attributes, transforming the component into the following:
1
render() {
2
return (
3
<form>
4
<TextField name="email" label="Email" id="email" type="email" />
5
<TextField name="password" label="Password" id="password" type="password" />
6
<input type="submit" value="Send" />
7
</form>
8
);
9
}
Copied!
We have just transformed long and potentially hard to read markup into a clean and precise render() method which only consists of very few components. If we ever wanted to add a change which should affect all of our text fields (for example adding a class), this can be added with ease by only changing data in one place — in the new TextField component.
Last modified 1yr ago