Created
July 8, 2018 11:33
-
-
Save ReactGirl/f7182a4a11e43360301617a4e34b296d to your computer and use it in GitHub Desktop.
understand bindActionCreators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| The bindActionCreators utility was specifically created for cases like binding methods for use in React components. In fact, straight from the bindActionCreators API reference: | |
| The only use case for bindActionCreators is when you want to pass some action creators down to a component that isn't aware of Redux, and you don't want to pass dispatch or the Redux store to it. | |
| Personally, though, I advise using the object shorthand argument for connect, instead of writing your own mapDispatch function. The object shorthand will automatically call bindActionCreators for you, and it's one less thing to worry about: | |
| export default connect(mapState, {addTodo, toggleTodo})(TodoList); | |
| So no, bindActionCreators isn't a "dangerous" pattern, and I highly recommend binding action creators so that your components aren't "aware" of Redux. When a component calls this.props.someFunction(), it shouldn't know or care whether that method is a callback from a parent component, a bound-up Redux action creator, or a mock function in a test. | |
| In addition, thunk action creators are absolutely a valid thing to dispatch. Again, the redux-thunk middleware is the entry-level way to handle async logic or complex synchronous logic that needs to access the store. I discussed the pros and cons of thunks in my blog post Idiomatic Redux: Thoughts on Thunks, Sagas, Abstraction, and Reusability. | |
| (Source: I'm a Redux maintainer.) | |
| https://www.reddit.com/r/reactjs/comments/6fxlaz/when_to_use_mapdispatchtoprops_vs_action_creator/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment