Learn how to inject dispatch() as a prop into a React component by using connect() from React Redux library.
Why does the AddTodo
component get dispatch
directly instead of a callback? Returning dispatch
from mapDispatchToProps
is a bit confusing... Is there any reason not to do this for consistency:
AddTodo = connect(null, dispatch => {
return {
onAddTodoClick : (text) => dispatch({ name : "ADD_TODO", text : text })
// I can't see the code from here so I may have left something
// out of that dispatch call
};
})(AddTodo)
Returning dispatch from mapDispatchToProps is a bit confusing
The default implementation of mapDispatchToProps
, if you don’t specify it, will return just { dispatch }
. I’m showing this pattern because it’s fairly common in examples, and it’s good to give it at least some exposure. In reality both styles work, and it’s really up to the person writing the code how they want to structure it.
Good, I had the same question :)