From the course: Learning Redux Toolkit
Unlock the full course today
Join today to access over 24,900 courses taught by industry experts.
Fetching API data with createAsyncThunk
From the course: Learning Redux Toolkit
Fetching API data with createAsyncThunk
- [Narrator] To handle asynchronous logic in bare Redux, you need to set up a middleware with an external package like a Redux doc. But Redux Toolkit provides a built-in createAsyncThunk method, for handling and processing asynchronous logic like API request, before supplying the result to our app reducers. To show you how this works, we'll create a local API using the JSON server from npx, and the data in our product list on JSON file. Let's go back to our terminal and run the command npx json-server, with the path to the product list on JSON file, to create a local API server. In our case, that'll be ./src/data /productlist .json. If you're asked to install JSON server, type Y for yes. With our API server created, we can navigate to localhost3000/products, and we'll see our product data gotten from the product list on JSON file. Let's go back to our code base and see how we can fetch data from this API,…