Quantcast
Channel: What is mapDispatchToProps? - Stack Overflow
Viewing all articles
Browse latest Browse all 7

What is mapDispatchToProps?

$
0
0

I was reading the documentation for the Redux library and it has this example:

In addition to reading the state, container components can dispatch actions. In a similar fashion, you can define a function called mapDispatchToProps() that receives the dispatch() method and returns callback props that you want to inject into the presentational component.

This actually makes no sense. Why do you need mapDispatchToProps when you already have mapStateToProps?

They also provide this handy code sample:

const mapDispatchToProps = (dispatch) => {
  return {
    onTodoClick: (id) => {
      dispatch(toggleTodo(id))
    }
  }
}

Can someone please explain in layman's terms what this function is and why it is useful?


Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images