0 votes
in ReactJS by

What is the difference between mapStateToProps() and mapDispatchToProps()?

1 Answer

0 votes
by

mapStateToProps() is a utility which helps your component get updated state (which is updated by some other components):

const mapStateToProps = (state) => {
  return {
    todos: getVisibleTodos(state.todos, state.visibilityFilter)
  }
}

mapDispatchToProps() is a utility which will help your component to fire an action event (dispatching action which may cause change of application state):

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

Recommend always using the “object shorthand” form for the mapDispatchToProps

Redux wrap it in another function that looks like (…args) => dispatch(onTodoClick(…args)), and pass that wrapper function as a prop to your component.

 const mapDispatchToProps = ({
   onTodoClick
 })

Related questions

0 votes
asked Dec 15, 2023 in ReactJS by rahuljain1
0 votes
asked Nov 5, 2023 in ReactJS by AdilsonLima
...