January 28, 2022

Blog @ Munaf Sheikh

Latest news from tech-feeds around the world.

Understanding the Container Component Pattern with React Hooks

The modern web development era offers a plethora of libraries and frameworks. These libraries and frameworks aim to help in rapid application development, better managing source code, speed and performance of the app, and many more.

React(aka React.js and Reactjs) is a JavaScript library that helps in building interactive user interfaces keeping the following aspects in perspective,

  • The developer focuses on the states of the application and can develop a view for each of the states.
  • The developer can create components to manage the states. It allows separating the presentational part from the complex business logic.
  • Virtual DOM usage helps create an in-memory cache to detect and compute the changes, and selectively update the DOM instead of performing a complete re-render. It helps in speed and performance.

While the library provides many useful features, developers need to know how to apply the solution to the well-known problems. Over the years, React developers have solved the problem of clean code, re-usability, and keeping things simple. All these solutions evolved as patterns. In this article, we will discuss a helpful and widely used pattern in React: Container Component pattern.

The Container Component Pattern

The Container Component pattern(or simply Component pattern) ensures to separate the data fetching logic, managing state from the presentational part. The presentational part has got a dumb name, dumb component. It is because the responsibility of the presentational part is just to render the information passed to it. The smart work of data fetching, working on it, and state management occurs elsewhere.

Traditionally, we used JavaScript classes to create containers. With the advent of hooks in React, the developer community has moved towards function-based components. We will take an example to understand the Container Component pattern end to end using React hooks.

The Movie App

We will build a movie app to fetch the movie information using an API call. We will create the user interface to show the movie details upon receiving the information.

Inslallation and Setup

Please use the Create React App tool to create an application. Let’s call it movie-app.

1npx create-react-app movie-app

Please note, You’ll need to have Node version >= 14.0.0 to run the above command successfully. Once done, please perform the following steps to run the app locally.

Change to the project directory

Run the app

You can also use yarn

You should get the app running @ http://localhost:3000/

Movie Data

For simplicity, we will keep the movie data in a JSON file. You can also use an actual movie API to fetch the information.

Please create a data folder under the src folder. Now create a file called movies.js file with the following content,

1export const movies = [

2 {

3 id: 1,

4 title: 'The Matrix',

5 year: 1999,

6 director: 'Lana Wachowski',

7 rating: 'R',

8 runtime: 136,

9 poster: 'https://images-na.ssl-images-amazon.com/images/M/MV5BNzQzOTk3OTAtNDQ0Zi00ZTVkLWI0MTEtMDllZjNkYzNjNTc4L2ltYWdlXkEyXkFqcGdeQXVyNjU0OTQ0OTY@._V1_SX300.jpg'

10 },

11 {

12 id: 2,

13 title: 'The Lord of the Rings',

14 year: 2001,

15 director: 'Peter Jackson',

16 rating: 'PG-13',

17 runtime: 178,

18 poster: 'https://images-na.ssl-images-amazon.com/images/M/MV5BN2EyZjM3NzUtNWUzMi00MTgxLWI0NTctMzY4M2VlOTdjZWRiXkEyXkFqcGdeQXVyNDUzOTQ5MjY@._V1_SX300.jpg'

19 },

20 {

21 id: 3,

22 title: 'The Dark Knight',

23 year: 2008,

24 director: 'Christopher Nolan',

25 rating: 'PG-13',

26 runtime: 152,

27 poster: 'https://images-na.ssl-images-amazon.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg'

28 },

29 {

30 id: 4,

31 title: 'Inception',

32 year: 2010,

33 director: 'Christopher Nolan',

34 rating: 'PG-13',

35 runtime: 148,

36 poster: 'https://images-na.ssl-images-amazon.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg'

37 },

38 {

39 id: 5,

40 title: 'Interstellar',

41 year: 2014,

42 director: 'Christopher Nolan',

43 rating: 'PG-13',

44 runtime: 169,

45 poster: 'https://images-na.ssl-images-amazon.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg'

46 },

47 {

48 id: 6,

49 title: 'The Dark Knight Rises',

50 year: 2012,

51 director: 'Christopher Nolan',

52 rating: 'PG-13',

53 runtime: 164,

54 poster: 'https://images-na.ssl-images-amazon.com/images/M/MV5BMTk4ODQzNDY3Ml5BMl5BanBnXkFtZTcwODA0NTM4Nw@@._V1_SX300.jpg'

55 },

56 {

57 id: 7,

58 title: 'The Lord of the Rings: The Fellowship of the Ring',

59 year: 2001,

60 director: 'Peter Jackson',

61 rating: 'PG-13',

62 runtime: 178,

63 poster: 'https://images-na.ssl-images-amazon.com/images/M/MV5BN2EyZjM3NzUtNWUzMi00MTgxLWI0NTctMzY4M2VlOTdjZWRiXkEyXkFqcGdeQXVyNDUzOTQ5MjY@._V1_SX300.jpg'

64 }];

This is a list of movies to use as an example. Please feel free to update it based on your choices.

The Movie Container

Now create a movies folder under the src folder. We will keep all the movies-app related source files under this folder.

Create a file MovieContainer.js under the movies folder with the following content,

1import { movies } from '../data/movies';


3const fetchMovies = () => {

4 return movies;



7const MovieContainer = () => {

8 console.log(fetchMovies());


10 return(

11 <div className="movie-container">

12 <h2>Movie Container</h2>

13 </div>

14 );



17export default MovieContainer;

Let’s understand what is in the above code. It is a simple React functional component.

  • We first import the movies data to use it.
  • The fetchMovies method pretends to create an abstraction to get the data from a store. In our case, it is JSON coming from a file.
  • We define the functional component MovieContainer. It logs the movies information on the browser console. It also renders a heading in the browser.
  • Finally, we export the component to import and use it elsewhere.

We will now use this component in the App.js file so that any changes we make in it reflect immediately in the UI. Please open the App.js and replace the content of it with the following code,

1import './App.css';

2import MovieContainer from './movies/MovieContainer';



5function App() {

6 return (

7 <div className="App">

8 <MovieContainer />

9 </div>

10 );



13export default App;

Here we import the MovieContainer and render it. Please visit the app in the browser. You should see the heading Movie Container to confirm our changes are working so far!


Also, if you observe the browser’s console, you will find the movies JSON data logged there.


Let’s now show the movies information in the UI.

Showing the Movie Information

Next, we will use the movies data to construct the user interface that will show the movie information. We will leverage React Hooks to manage the state and lifecycle using functional components.

React Hooks are a great addition to the library that allows managing state and many other features without writing JavaScript classes. You can read about the motivation of introducing hooks in React from here.

For the movie-app, we will use two built-in React hooks.

  • useState: It helps in managing the local state so that React can preserve and use the state between re-renders. The useState hook returns the current state value and a function to update the state value. We can also pass the initial state value as a parameter to the useState() hook.

    1const [movies, setMovies] = useState([]);

    Learn more about this hook from here.

  • useEffect: The useEffect hook helps in performing side-effects. The side-effect can be anything responsible for changing the state of your application. A typical example of a side-effect is making an API call and changing the local state to preserve the response data.

    1useEffect(() => {


    3 setMovies(movies);

    4}, []);

    Learn more about this hook from here.

Let us get back to the movies-app with everything we have learned about the above hooks. Move over to the MovieContainer.js file and import the useState and useEffect hooks.

1import { useState, useEffect } from 'react';

Next, we will create a local state using the useState hook. Please add the following line in the MovieContainer component function.

1const [movies, setMovies] = useState([]);

Here we create a local state called movies initialized with an empty array([]). The setMovies is a method to update the movie’s information. We will do that as a side-effect inside the useEffect hook.

Please add the following code snippet after the useState code you have added previously.

1useEffect(() => {

2 const movies = fetchMovies();

3 setMovies(movies);

4 }, []);

Here we are getting the movies data(from the JSON) and updating the state (movies) using the setMovies() method. A couple of things to note here,

  • The fetchMovies() method is an abstraction. Here we are fetching the local JSON data. We can change the method’s logic to fetch it from an actual API.
  • The empty array([]) we pass to useEffect hook to inform React about calling the side-effect only once when the component loads. The effect doesn’t depend on any states to re-run again in the future.

Now let us show the movies data in the UI. To do that, please use the following JSX code in the return.

1<div className="movie-container">

2 <h2>Movies</h2>

3 <ul className ="movie-list">

4 {movies.map(movie => (

5 <li key={movie.id} className="movie">

6 <img src={movie.poster} alt={movie.title} />

7 <p>{movie.title} by {movie.director} was released on {movie.year}</p>

8 <p>Rating: {movie.rating}</p>

9 </li>

10 ))}

11 </ul>


Great!!! Now our movie-app should render the movies data.

Initial data in UI

We see the data in the UI but the look-and-feel is not that great. Let us add some CSS to fix that. Please create file called movies.css under src/movies folder with the following content.

1li {

2 list-style: none;



5.movie-container {

6 display: flex;

7 flex-direction: column;



10.movie-list {

11 display: flex;

12 flex-direction: row;

13 flex-wrap: wrap;

14 align-content: center;

15 justify-content: center;



18.movie {

19 flex: 1;

20 margin: 10px;

21 padding: 10px;

22 border: 1px solid #ccc;

23 border-radius: 5px;

24 background-color: #fff;



27.movie img {

28 align-self: center;


The final thing is to import the movies.css file into the MovieContainer.js file

1import './movies.css';

Bingo!!! The movie-app UI looks much better now.

data load with styles

Here is the complete code of the MovieContainer.js file so far.

1import { useEffect, useState } from 'react';

2import { movies } from '../data/movies';

3import './movies.css';



6const fetchMovies = () => {

7 return movies;



10const MovieContainer = () => {

11 console.log(fetchMovies());


13 const [movies, setMovies] = useState([]);


15 useEffect(() => {

16 const movies = fetchMovies();

17 console.log('MovieContainer: useEffect: movies: ', movies);

18 setMovies(movies);

19 }, []);


21 return(

22 <div className="movie-container">

23 <h2>Movies</h2>

24 <ul className ="movie-list">

25 {movies.map(movie => (

26 <li key={movie.id} className="movie">

27 <img src={movie.poster} alt={movie.title} />

28 <p>{movie.title} by {movie.director} was released on {movie.year}</p>

29 <p>Rating: {movie.rating}</p>

30 </li>

31 ))}

32 </ul>

33 </div>

34 );



37export default MovieContainer;

All the code we have written so far helps us show the movie information in the UI. We have accomplished our goal, but we have plenty of room for improvement.

  • First, we have the data fetching and the presentation code mixed together. It means, if we want to use the same presentation logic elsewhere, we have to repeat it. We need to avoid this.
  • Can we make the data fetching logic a bit more abstract so that any presentation can use this data in various formats?

We will use the Container Component pattern to make the first improvement. We will discuss the second improvement when discussing the Higher-Order component pattern in a future article.

The Container Component Pattern Kicks in

We separate the data fetching logic from the presentational logic with the Container Component pattern. We create as many dumb presentational components as required and pass data to them. Let’s apply this pattern to the movie-app.

The core presentation logic of the movie-app iterates through the movie information and creates UI elements for each movie. We can create a new presentation (dumb) component called Movie and pass each movie information.

Please create a file called Movie.js under the src/movies folder with the following content,

1const Movie = ({movie}) => {

2 const {

3 title,

4 year,

5 poster,

6 rating,

7 director} = movie;


9 return (

10 <li className="movie">

11 <img src={poster} alt={title} />

12 <p>{title} by {director} was released on {year}</p>

13 <p>Rating: {rating}</p>

14 </li>

15 );



18export default Movie;

This presentational component takes the details of each movie as props and uses the values to create the UI presentations. Now we will use the Movie component into the MovieContainer component.

First, import the Movie component at the top section of the MovieContainer component.

1import Movie from './Movie';

Now replace the <li>...</li> section of the MovieContainer.js file with the following code,

1<Movie movie={movie} key={movie.id} />

That’s all. Now the MovieContainer component is much smaller than before. We have also separated out the presentation logic from the data fetching part. After the changes, here is the complete code of the MovieComponent.js file.

1import { useEffect, useState } from 'react';

2import { movies } from '../data/movies';

3import Movie from './Movie';

4import './movies.css';



7const fetchMovies = () => {

8 return movies;



11const MovieContainer = () => {

12 console.log(fetchMovies());


14 const [movies, setMovies] = useState([]);


16 useEffect(() => {

17 const movies = fetchMovies();

18 console.log('MovieContainer: useEffect: movies: ', movies);

19 setMovies(movies);

20 }, []);


22 return(

23 <div className="movie-container">

24 <h2>Movies</h2>

25 <ul className ="movie-list">

26 {movies.map(movie => (

27 <Movie movie={movie} key={movie.id} />

28 ))}

29 </ul>

30 </div>

31 );



34export default MovieContainer;

If we see it in a pictorial representation, it may look like this:

one level

Do you think we should refactor the code a bit more to create another parent-level presentation component? Well, we can. We can create a presentation component called MovieList(taking the <ul>...</ul> part), and that can use the Movie Component. Finally, we can use the MovieList component in the MovieContainer. It may look like this:

two level

It all depends on how far you think you should go keeping the re-usability and separation of concern in mind. I hope we understand the idea behind the Container Component pattern.

Open Source Session Replay

Debugging a web application in production may be challenging and time-consuming. OpenReplay is an Open-source alternative to FullStory, LogRocket and Hotjar. It allows you to monitor and replay everything your users do and shows how your app behaves for every issue.
It’s like having your browser’s inspector open while looking over your user’s shoulder.
OpenReplay is the only open-source alternative currently available.


Happy debugging, for modern frontend teams – Start monitoring your web app for free.


As we mentioned before, the patterns are evolved from the solution of repeated problems in the past. You can choose to adopt, tweak, or even not use it.

A pattern such as Container Component pattern helps in achieving the principles of,

  • Separation of Concern(SoC) by keeping the data fetching and presentation away from each other.
  • Don’t Repeat Yourself(DRY) by providing us the ability to reuse a component multiple times in different places.

Like this one, we have other patterns like Higher-Order Component(HoC) pattern, Render Props, and many more. We will see them soon in the upcoming articles. Stay tuned.

You can find all the source code used in this article from here,

The movie-app project on GitHUb

Before we end…

I hope you found this article insightful and informative. My DMs are open on Twitter if you want to discuss further.

Let’s connect. I share my learnings on JavaScript, Web Development, and Blogging on these platforms as well:

Source link