DEV Community

Cover image for React Project: Movie App

Posted on

React Project: Movie App

Hello πŸ‘‹, For my first post on, I decided to share with you a small tuto about creating a Movie App with React and TypeScript.

Demo πŸš€

Source code available on Github : Here !


Before starting coding let's setup our project.

Generate project with CreateReactApp:

I often (to not say always 😁) use Create React App to initiate my React projects.

In order to generate our project run :

npx create-react-app my-movie-app --template typescript
Enter fullscreen mode Exit fullscreen mode


The frontend will have to fetch the data from an API, I choose TMDb 🎬 : It's free, you just need to create an account to get your API key

Please use your API key as an environment variable, in .env file :

Enter fullscreen mode Exit fullscreen mode

The configuration is done, let's start coding. πŸ’»

State Management

Our app components are going to communicate between them.
To handle this, we need a state management, for that, we are going to combine the context API and state hooks

In case, you are not familiar with react hooks. I encourage you, to read ➑️ React Hooks and Context API

// create context
export const MoviesContext = React.createContext<{
  movies: Movie[];
  updateMovies: Function;
  movies: [],
  updateMovies: Function,

function App() {
// get default values
  useEffect(() => {
      .catch((_) => setMovies([]));
  }, []);

// use state hook
  const [movies, setMovies] = useState<Movie[]>([]);

  return (
  // Provide to the all project: 
  // - movies an array of Movie, 
  // - updateMovies : function to update the movies list
    <MoviesContext.Provider value={{ movies, updateMovies: setMovies }}>
      <div className="App">

Enter fullscreen mode Exit fullscreen mode


Don’t be afraid to split components into smaller components. (React Doc)

In React it's important to split the UI by components, so let's check how many components do we need :

As you can see, three components are standing out:

  • Header
  • Search
  • Catalog

A question to ask when creating a component:
What my component should do ?! πŸ€”


The header component is pretty simple, it contains the project title and the Search component.

export const Header = () => {
  return (
    <div className="header">
      <h1 className="header__title">Movie App</h1>
      <div className="header__search">

Enter fullscreen mode Exit fullscreen mode


The search component should:

  1. show an input field
  2. save user search
  3. query the API with this search
  4. update the movie list

In order to save the state of our input, we will use in this component the useState hook.

export const Search = () => {
  const [search, setSearch] = useState("");
  // Consume our context to get updateMovies function
  const { updateMovies } = useContext(MoviesContext); 

  const handleOnSubmit = (event: React.FormEvent) => {
    if (search) {
      searchMovies(search).then((movies) => {

  return (
      <form name="form" onSubmit={(e) => handleOnSubmit(e)} noValidate>
          placeholder="Search movie ... "
          onChange={(e) => setSearch(}

Enter fullscreen mode Exit fullscreen mode


The catalog component should:

  1. get movies from the context
  2. loop on this array
export const Catalog = () => {
  // Consume the context to get list of movies.
  const { movies } = useContext(MoviesContext);

  return (
    <div className="catalogContainer">
      { => (
        <div className="catalog__item" key={}>
          <div className="catalog__item__img">
            <img src={movie.picture || imgPlaceholder} alt={movie.title} />
            <div className="catalog__item__resume">{movie.resume}</div>
          <div className="catalog__item__footer">
            <div className="catalog__item__footer__name">
              {movie.title} ({new Date(})
            <div className="catalog__item__footer__rating">{movie.rating}</div>

Enter fullscreen mode Exit fullscreen mode

fetch() data from API

In order to fetch the API for data, let's create a service :

const movieApiBaseUrl = "";

export function searchMovies(search: string): Promise<Movie[]> {
  return fetch(
    .then((res) => res.json())
    .catch((_) => {
      return [];

export function discoverMovies(): Promise<Movie[]> {
  return fetch(
    .then((res) => res.json())
    .then((response) => mapResult(response.results))
    .catch((_) => {
      return [];
Enter fullscreen mode Exit fullscreen mode


With this tutorial, I tried to present to you some important concept like :

  • React Context API
  • React hooks
  • function fetch to make API calls

To go further

Some more ideas to improve your skills:

  • handle No result
  • use Redux
  • pagination

Source :

  • You can clone this project on Github

Discussion (4)

cayde profile image


maj07 profile image
Majuran SIVAKUMAR Author


mrmetaverse profile image

Gonna try this out! Might be a fun baseline for an XR app were building. Cheers!

maj07 profile image
Majuran SIVAKUMAR Author

Thanks for reading πŸ‘