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.

getState in mapDispatchToProps

See original GitHub issue

Since mapDispatchToProps can expose the dispatch method, it means it has access to the store. I was wondering why it didn’t also expose, at least, the getState method?

Just like redux-thunk, you might need to access the current state to decide which action to actually dispatch regarding client side cache.

I’m fine with doing a PR but wanted to check if it would be valid. Also, if accepted, I would rather have [mapDispatchToProps(dispatch, [getState], [ownProps]): dispatchProps] signature, but it would be a breaking change compared to [mapDispatchToProps(dispatch, [ownProps], [getState]): dispatchProps]

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:20 (10 by maintainers)

github_iconTop GitHub Comments

16reactions
gaearoncommented, Feb 19, 2016

Have you looked at Redux Thunk?

It lets you write stuff like

function getSuggestions(query) {
  return (dispatch, getState) => {
    if (getState().suggestions[query]) {
      return;
    }

    return API.getSuggestions().then(
      response => dispatch({ type: 'SUGGESTIONS_RESPONSE', response }),
      error => dispatch({ type: 'SUGGESTIONS_ERROR', error })
    )
  };
}

// in component

class Stuff extends Component { /* ... */ }

export default connect(
  mapStateToProps,
  { getSuggestions }
)(Stuff)
5reactions
jimbollacommented, Mar 8, 2017

mapDispatchToProps will never be dependent on state, because doing so would mean it would get invoked every time the store state changes, creating new functions each time, causing the component to rerender every time. mapDispatchToProps already does a lot of logic to minimize this when props change, which is bad enough. I personally never even use the version that accepts props, to avoid unnecessary rerenders. You’re a million bajillion times better off exposing a few props you don’t really need than generating new functions and renders on every state change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access State inside of mapDispatchToProps method
I think the real use case for accessing the state in mapDispatchToProps is to know which actions are available at runtime. For example...
Read more >
Connect: Dispatching Actions with mapDispatchToProps
Providing a mapDispatchToProps allows you to specify which actions your component might need to dispatch. It lets you provide action dispatching ...
Read more >
MapStateToProps and MapDispatchToProps in React Redux
In this video you will learn such important things to work with Redux is mapStateToProps and mapDispatchToProps. Let's jump right into it.
Read more >
3 ways to test mapStateToProps and mapDispatchToProps
You already know that you shouldn't actually test the result of calling connect (the connected component) - as Redux already makes sure the ......
Read more >
Redux No Redux Example - StackBlitz
const { getState, dispatch } = useContext. (Context);. const stateProps = mapStateToProps(getState. (), ownProps);. const dispatchProps = mapDispatchToProps.
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