I can't seem to fully understand React-Redux... #139778
-
BodyI've recently been reading about Redux and it's benefits and use cases to include in my React projects. After getting past the deep legacy code for one simple API call or state management action, I'm stuck in trying to figure out the difference between using "createAsyncThunk" and "createApi" from the npm package "@reduxjs/toolkit". They both seem to do the same thing - make an API call to some external link, perform CRUD, update the state, and update the React component. I've manage to accomplish the exact same result with writing code for both functions. For "createAsyncThunk", I have to make a slice with "createSlice" and pass it into the reducer in the app's store.js file. For "createApi", the same holds true, except I have to also add it into the middleware. So, long story short, I am uncertain of the true benefits of choosing one over the other. I'm working with React 18, React-Redux 9, and Redux-Toolkit 2 (latest as of this date) Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
1.
|
Beta Was this translation helpful? Give feedback.
Hi @MarshallNickolauson
1.
createAsyncThunk
:createAsyncThunk
inside your slice, and you can handle success, error, or loading states in your reducers.2.
createApi
:createApi
from@reduxjs/toolkit/query
is more specialized for managing multiple API calls and handling CRUD operations. It automaticall…