Displaying Dynamic Data in React Components

Tutorial 4 of 5

1. Introduction

In this tutorial, we'll explore how to display dynamic data in React components. Dynamic data is data that changes and is not hardcoded into the application. This data can be fetched from an API (Application Programming Interface) and displayed within our React components.

By the end of the tutorial, you'll learn:
- How to fetch data from an API using fetch or axios.
- How to pass this data into components.
- How to render this data in a structured and organized manner.

Prerequisites:
- Basic understanding of JavaScript and React.
- Node and npm installed on your machine.

2. Step-by-Step Guide

React components use state and props to manage and handle data. We'll mainly use the state to store our dynamic data and props to pass data to child components.

  1. Fetching Data: We can fetch data from an API using fetch or axios. This is usually done in the componentDidMount lifecycle method (for class components) or the useEffect hook (for functional components).

  2. Storing Data: After fetching the data, we store it in the component's state.

  3. Passing Data: If we want to pass the data to a child component, we can pass it as props.

  4. Rendering Data: Inside our render method (or return statement for functional components), we can map over our data and display it in our JSX.

3. Code Examples

Here's an example of a functional component fetching data from a JSON placeholder API and displaying it.

import React, { useState, useEffect } from 'react';
import axios from 'axios';

function DataComponent() {
  // Initialize state to hold our data
  const [data, setData] = useState([]);

  // Fetch data from API
  useEffect(() => {
    axios.get('https://jsonplaceholder.typicode.com/posts')
      .then(response => {
        // Set the data in state
        setData(response.data);
      })
      .catch(error => {
        console.error('Error fetching data: ', error);
      });
  }, []); // Empty array means this effect runs once on mount

  // Render data
  return (
    <div>
      {data.map(item => (
        <div key={item.id}>
          <h2>{item.title}</h2>
          <p>{item.body}</p>
        </div>
      ))}
    </div>
  );
}

export default DataComponent;

In this example, we import useState and useEffect from React and axios for fetching data. We initialize our state with an empty array using useState. In the useEffect hook, we fetch the data and set it into our state. Finally, we map over our data and display it in our JSX.

4. Summary

In this tutorial, we learned how to fetch, store, pass, and render dynamic data in React components. We understood the use of useState and useEffect hooks in functional components, and how to render a list of items in JSX.

For further learning, you can look into error handling during API calls, using other hooks like useContext and useReducer for better state management, and exploring other libraries for fetching data like react-query and SWR.

5. Practice Exercises

  1. Exercise: Fetch data from this API https://jsonplaceholder.typicode.com/users and display the users' names and email addresses.

  2. Exercise: Fetch data from an API of your choice and display it in a table format.

  3. Exercise: Add pagination to the data fetched from the API.

Solutions will depend on the exact API used and how you choose to display the data.

Happy coding!