in React JS by
Q:

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
 })
Click here to read more about React JS
Click here to read more about Insurance

Related questions

0 votes
asked Mar 30, 2020 in React JS by amita rallin
0 votes
asked Mar 3, 2020 in React JS by miceperry
0 votes
asked Mar 1, 2020 in React JS by RShastri
0 votes
asked Jul 2, 2019 in React JS by Venkatshastri
0 votes
asked Nov 26, 2019 in React JS by AdilsonLima
...