Redux-Saga with Redux-Toolkit | Movie Search App

ref – https://www.youtube.com/watch?v=DPOzlL1fpnU

npx create-react-app movie-app

npm install @mui/material @emotion/react @emotion/styled

npm install @mui/material react-redux @reduxjs/toolkit redux-saga react-router-dom axios

First, we have a simple Promise that fetches data from the server.
This is a non-blocking operation, because it involves I/O with the web environment.
Thus when we asynchronously execute the fetch, we can let it process and go on to other tasks.

src/redux/api.js

Since we asynchronously execute the fetch, we use call. This is because call stops the saga middleware and waits for its operation to finish.
Once we get the response, then we continue on. fetchMovies still processes it asynchronously.

Finally, we use put (non-blocking) to put the data in the store. (We have received the data already, so storing data can be done asynchronously here)

src/redux/movieSagas.js

and executes onLoadMoviesAsync when it receives that message.

Notice that takeLatest listens for getMovies.type, which logs ‘movie/getMovies’. But how did it create this tring?

It takes the name property of the slice:

and then append the action name “getMovies” behind it.

src/redux/feature/movieSlice.js

If you log getMovies, which is a function reference, you’ll see that it creates an action for you:

type is passed in from createAction from another execution so we know that we’re being used as a curried function.

Run the app and we start with initial state:

Then we do a get movies, which returns the initial state:

We run our dispatch getMovies in useEffect of our Search component, which has default value ‘spider’ and get a return of all movies with spider keyword.

movie-app demo part 1

– 38:00 at the video