Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use Redux Thunk [closed]

Why use Redux Thunk then one can do something like this:

ReadableAPI.getCategories().then((categories)=>{
      console.log('after getCategories', categories)

      this.props.dispatch(addCategories(categories))
    })

Isn't this more straightforward and achieves the same thing?

like image 553
preston Avatar asked Oct 31 '17 08:10

preston


People also ask

Why we should use redux-thunk?

It allows us to return functions instead of objects from redux actions. Plain redux doesn't allow complex logic inside action functions, you can only perform simple synchronous updates by dispatching actions. This middleware extends its ability and lets you write complex logic that interacts with the store.

Do we really need redux-thunk?

Writing the component will be more native, with React syntax. No more mapDispatch , mapState , and other connecting things. Another thing is, with this approach, we don't event need Redux Thunk to handle the async action. We just call them directly in an action and dispatch an action when the async action is resolved.

Why is redux-thunk better than Redux?

But on the other hand, for bigger projects, Redux-Thunk may sometimes get you in trouble, as it can be hard to scale if your side effect or asynchronous logic increases, whereas in the case of Redux-Saga, it comes power-packed with some amazing things such as concurrent side effects, canceling side effects, debouncing ...

Is redux-thunk synchronous?

Thunk in Redux 🍔By default actions in Redux are dispatched synchronously, which is a problem for any SPA that needs to communicate with an external API or perform side effects.


1 Answers

Redux Thunk basically allows us to delay the dispatch of an action, i.e we can handle the action returned by the action creator and call the dispatch function when we really want to dispatch.

like image 135
Saptarshi Dey Avatar answered Sep 28 '22 13:09

Saptarshi Dey