What is the difference between store.dispach, useDispatch and dispatch from useReducer?

Viewed 1929
import React, { useReducer } from "react";

...

import { useDispatch } from "react-redux";

...

import { createStore } from "redux";

const store = createStore(

... I see all trigger the actions but useReducer dispatch don't update the redux state.

1 Answers

Correct, useReducer does not update a redux store. useReducer is a regular react hook, and allows a component to apply local state updates in a functional way, similar to the pattern used by Redux, a very common global app state management library. Think of useReducer as component state version of Redux, which is app state.

useDispatch is a hook provided by react-redux that consumes a redux context (containing the store/state), similar to the functionality that the connect Higher Order Component provides when wrapping a component. These both provide a dispatch function to dispatch actions to the redux store.

store.dispatch is the dispatch function.

Note: react-redux connect HOC uses redux's bindActionCreators to wrap(bind) the dispatch function to your action creators defined in mapDispatchToProps.

Related