The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

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

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

4:41
Learn how to inject dispatch() as a prop into a React component by using connect() from React Redux library.
Watch this lesson now
Avatar
egghead.io

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

Avatar
Sequoia McDowell

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)
Avatar
Dan Abramov

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.

In reply to Sequoia McDowell
Avatar
Ricardo Mayerhofer

Good, I had the same question :)

In reply to Dan Abramov
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?