DEV Community

Discussion on: Opinionated React: State Management

Collapse
 
friedensstifter profile image
friedensstifter

Hey Faraz,
great Impressions for a React Newbi :) Thanks for your six parts, hope more comming soon. The Enumeration of Status is a great idea. I've used 3 booleans for checking state of loading and showing a message :)

But i have a question about how you query your MovieService. This looks pretty awesome how you call your functions:
MovieService
.fetchInitialMovies()
.then(initialMovies => setMovies(initialMovies))
.then(() => setIsLoading(false))

Can you show me how your MovieService works? Haven't found any code of your MovieService :(

Thanks :)

Collapse
 
farazamiruddin profile image
faraz ahmad • Edited

Hey! Sure. I made a mock service just for learning purposes. I have some hard-coded values that I return using a setTimeout and a Promise.

Here it is:

import { Movie } from "../types/movie";

const MOVIES: Movie[] = [
  { id: 1, title: "Iron Man" },
  { id: 2, title: "The Incredible Hulk" },
  { id: 3, title: "Iron Man 2" },
  { id: 4, title: "Thor" },
  { id: 5, title: "Captain America: The First Avenger" },
  { id: 6, title: "The Avengers" }
];

export class MovieService {
  static fetchMovieTitles(): Promise<Movie[]> {
    return new Promise(resolve => {
      return setTimeout(() => resolve(MOVIES), 2000);
    });
  }
}

What this does is returns a list of movies after 2000ms.