generator-react-webpack-redux icon indicating copy to clipboard operation
generator-react-webpack-redux copied to clipboard

Include redux-thunk?

Open MarcoScabbiolo opened this issue 7 years ago • 8 comments

Given that it is essential if you want to use asynchronous operations in actions, I think it would be nice to include redux-thunk. Maybe it's just me, but I end up adding it to every proyect. It is a very small dependency and as a middleware it's not invasive at all.

On top of including redux-thunk as a dependency, these would be the changes needed:

At generators/root/templates/store.js:

Add

import thunk from 'redux-thunk';

Change import { createStore } from 'redux'; To import { createStore, compose, applyMiddleware } from 'redux';

And change:

const store = createStore(reducers, initialState,
    window.devToolsExtension && window.devToolsExtension());

To:

const store = createStore(reducers, initialState, compose(
  applyMiddleware(thunk),
  window.devToolsExtension ? window.devToolsExtension() : f => f
));

It could be implemented optionally with a prompt. What do you think? I can make the pull request if you're ok with this request.

MarcoScabbiolo avatar Mar 22 '17 17:03 MarcoScabbiolo

Hey,

I'm not sure I agree that it's essential. I use redux-saga, and at a pinch you could get away with just using promises and closures.

I have thought of making a generator that creates sagas in the same style that this generator does for containers etc, but just putting it out as a normal yeoman generator.

Perhaps you could do the same with redux-thunk?

andytango avatar Mar 22 '17 17:03 andytango

@andytango The difference with redux-saga and redux-thunk is that the latter is very straight forward and no additional sub-generators would be needed to take full advantage of it, all you need is to import it as a middleware. Maybe a prompt "Would you like to use redux-thunk" that defaults to "no" could do the trick. It's not that redux-thunk is not widely used, it is mentioned in the official Redux docs.

MarcoScabbiolo avatar Mar 22 '17 17:03 MarcoScabbiolo

Hey @MarcoScabbiolo yes I think those are fair points

+1

andytango avatar Mar 22 '17 17:03 andytango

In fact this has been on my mind for quite some time. During setup as @MarcoScabbiolo describes.

Will see what I can do.

@MarcoScabbiolo if you by any chance already built that, feel free to submit a PR ;-)

stylesuxx avatar Mar 23 '17 11:03 stylesuxx

@stylesuxx I don't have it ready yet, but I have some experiencie with generators and Inquirer, I might be able to get it done this weekend, If you are not already doing it let me know and i'll submit the PR when it's ready.

MarcoScabbiolo avatar Mar 23 '17 14:03 MarcoScabbiolo

@MarcoScabbiolo, @stylesuxx I think we didn't need thunk in this generator, because in this generator we are using bindActionCreators and according to documentation bindActionCreators wrapped into a dispatch call so they may be invoked directly. am I right ?

umair-khanzada avatar Apr 01 '17 07:04 umair-khanzada

@umair-khanzada bindActionCreators calls your action creators forwarding the arguments and passes the resulting action to dispatch, it doesn't pass dispatch to the action creator to turn it into a thunk, for that you need redux-thunk or redux-saga or redux-loop. Unless there is something else going on in bindActionCreators but the documentation doesn't say anything about that.

MarcoScabbiolo avatar Apr 01 '17 09:04 MarcoScabbiolo

@MarcoScabbiolo Yeah you are right I was confused, both are different concept, bindActionCreators simply wrap action into dispatch.

umair-khanzada avatar Apr 12 '17 06:04 umair-khanzada