When your components are initialized, you'll want to populate their state or properties. Otherwise, the component won't have anything to render other than its skeleton markup. For instance, let's say you want to render the following user list component:
import React from "react";
const ErrorMessage = ({ error }) => (error ? <strong>{error}</strong> : null);
const LoadingMessage = ({ loading }) => (loading ? <em>{loading}</em> : null);
export default ({ error, loading, users }) => (
<section>
<ErrorMessage error={error} />
<LoadingMessage loading={loading} />
<ul>
{users.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
</section>
);
There are three pieces of data that this JSX relies on, as follows:
- loading: This message is displayed while fetching API data.
- error: This message is displayed if something goes wrong.
- users...