1. 28
    Redux: Generating Containers with connect() from React Redux (AddTodo)
    4m 41s

Redux: Generating Containers with connect() from React Redux (AddTodo)

Share this video with your friends

Send Tweet

Learn how to inject dispatch() as a prop into a React component by using connect() from React Redux library.

Sequoia McDowell
Sequoia McDowell
~ 9 years ago

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)
Dan Abramov
Dan Abramov(instructor)
~ 9 years ago

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.

Ricardo Mayerhofer
Ricardo Mayerhofer
~ 9 years ago

Good, I had the same question :)