getState in mapDispatchToProps
See original GitHub issueSince 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:
- Created 8 years ago
- Reactions:1
- Comments:20 (10 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Have you looked at Redux Thunk?
It lets you write stuff like
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.