question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request use mapStateToProps in mapDispatchToProps

See original GitHub issue

Hello

First of all I would thank you for this awesome library been using it in all my projects lately!

There is just one thing that is bugging me. I often would like to use the state props I pick from the mapStateToProps in mapDispatchToProps. Right now I use a library called recompose and a hoc called withHandlers to do this.

Example how I do today.

compose(
  connect(
    (state) => ({
      someProp: state.someState,
    }),
    (dispatch) => ({
      onSomeAction: (someProp) => dispatch(someAction(someProp)),
    }),
  ),
  withHandlers({
    onSubmit: ({ someProp, onSomeAction }) => (e) => {
      e.preventDefault();
      onSomeAction(someProp);
    },
  }),
)(DumbComponent);

Suggested feature.

connect(
  (state) => ({
    someProp: state.someProp,
  }),
  (dispatch) => ({
    // If the dispatch prop is executed and returns a functions then run that function automatically with the latest props
    onSubmit: ({ someProp }) => (e) => {
      e.preventDefault();
      dispatch(someAction(someProp));
    },
  }),
)(DumbComponent);

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:17 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
timdorrcommented, Sep 5, 2017

I’d just use the getState arg in redux-thunk, personally. Way more testable that way.

0reactions
markeriksoncommented, Sep 5, 2017

Mmm… I may be mixing up cases in my head between using the object shorthand and using an actual mapDispatch function. I’ll have to look at this again later.

I still stand by my point that I don’t think this is worth another edge case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mapStateToProps and mapDispatchToProps: getting IDE ...
I haven't been using React recently. As for a solution, see the original question under "Motivation". That workaround allows for IDE code ...
Read more >
You Might Not Need The mapDispatchToProps Function
First, a mapStateToProps function that plucks pieces of state out of Redux and assigns them to props that your React component will use....
Read more >
Connect: Extracting Data with mapStateToProps
As the first argument passed in to connect , mapStateToProps is used for selecting the part of the data from the store that...
Read more >
Use Redux with React
Both mapStateToProps and mapDispatchToProps are optional and can be omitted as necessary. If you're using a selector that is produced through a factory, ......
Read more >
Demystifying 15 lesser-known React Redux terms and ...
Compromises of using Redux over Flux. Difference between mapStateToProps and mapDispatchToProps. Dispatching action in the reducer.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found