Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between using redux-thunk and calling dispatch() directly

I'm in the learning phase of understanding redux state management and still trying to negotiate the bewildering jungle of boilerplate code and middleware, much of which I'm taking on faith as 'good medicine'. So I hope you'll bear with me on this perhaps rudimentary question.

I know that redux-thunk allows action creators to proceed asynchronously and dispatch a regular action at a subsequent time. For example, I can define a thunk action creator in my actions.js:

export function startTracking() {
  return (dispatch => {
     someAsyncFunction().then(result => dispatch({
       type: types.SET_TRACKING,
       location: result
     }))
  })
}

And invoke it from within a React component like so:

onPress={() => this.props.dispatch(actions.startTracking())}

My question is, what advantage does the above code confer over simply dispatching an action from inside an asynchronous callback?

import { store } from '../setupRedux'
...

export function startTracking() {
 someAsyncFunction().then(result => {
   store.dispatch({
     type: types.SET_TRACKING,
     location: result
   })
 })
}

which I would invoke inside my component

onPress={() => actions.startTracking()}

or even

onPress={actions.startTracking}

Is there anything problematic with accessing store directly via an import as I'm doing in the 2nd example?

like image 396
pscl Avatar asked Dec 12 '17 19:12

pscl


People also ask

What is difference between Redux thunk and Redux?

Redux and redux-thunk can be categorized as "State Management Library" tools. Redux and redux-thunk are both open source tools. It seems that Redux with 49.5K GitHub stars and 12.8K forks on GitHub has more adoption than redux-thunk with 12.6K GitHub stars and 683 GitHub forks.

What is dispatch in Redux thunk?

Redux Thunk is a middleware that lets you call action creators that return a function instead of an action object. That function receives the store's dispatch method, which is then used to dispatch regular synchronous actions inside the function's body once the asynchronous operations have been completed.

Why is redux thunk better than Redux?

The benefit of Redux-Saga in comparison to Redux-Thunk is that you can more easily test your asynchronous data flow. Redux-Thunk, however, is great for small projects and for developers who just entered into the React ecosystem. The thunks' logic is all contained inside of the function.

What is the use of dispatch in Redux?

dispatch is a function of the Redux store. You call store. dispatch to dispatch an action. This is the only way to trigger a state change.


1 Answers

There is nothing wrong doing so. From the redux-thunk page:

If you’re not sure whether you need it, you probably don’t.

The creator of redux explain the advantage of using it here:

This looks simpler but we don’t recommend this approach. The main reason we dislike it is because it forces store to be a singleton. This makes it very hard to implement server rendering. On the server, you will want each request to have its own store, so that different users get different preloaded data.

Basically, using redux-thunk will save you the store import in each action creator file and you will be able to have multiple store. This approach also give you the opportunity to write a little bit less code and to avoid spaghetti code. Many redux developer don't like to import the store and to manually dispatch because it can create circular dependencies if the code is badly separated (importing an action name from the action creator file in the reducers file and importing the store from the reducers file in the action creator file). Also, dispatching directly an action like that might break the middleware workflow, ie: the action might not be handled by a middleware.

But honestly, if you don't see an advantage to it yet, don't use it. If one day you have trouble with async actions, redux-thunk might be the answer.

like image 152
Vincent D'amour Avatar answered Nov 15 '22 04:11

Vincent D'amour