Use the React-Redux Hooks API

Tram Ho

In this article I will share how to use the React-Redux Hooks . Firstly, let’s clarify a little Flow of React-Redux project that we are still familiar with.

redux, action, reducer, store, …

We have a state with count = 0, count increases by 1 every time the store perform an action dispatch can type = INCREASE and decreases once the action to dispatch with type = DECREASE. Let’s say you understand the basic process of redux with action , reducer , because this article is for those who already know how to use redux .

Provider, connect

redux provides a state management tool for javascript applications, so it’s not just for react , to connect react with redux , we need to use the react-redux library, first we need to “transmit” the store we have just created into the main component of the application.

Provider is an react-redux API , using React ‘s Context API makes it possible for all components in the application’s Component Tree to access the store we created, so it will be rendered above a “level”. for App – the main component of the application.
And if Provider acts as Context.Provider then connect is Context.Consumer, api we will use to “receive” state from the store and then transmit the action to be declared in each component needed as props.
We have the following App component :

OK, using redux is very basic and normal, but I have to admit that I don’t like to connect very much, firstly because the syntax is relatively confusing, especially for newbies, I’ve also fainted up and down to understand and Familiar with mapStateToProps or matchDispatchToProps , the second because connect uses the higher order component (hocs) so it creates multiple layers for the application’s component tree .

hooks api

react hooks allow us to develop react applications more simply, without using component classes with complex lifecycle systems, custom hooks allow us to create reusable states without using Hocs . Simple to understand, using hooks , the code is cleaner, much easier to understand.
react-redux recent version also provides API hooks that make it much easier for us to connect , and there is no need to use connect anymore, first I will refactor the App with hooks .

hmm, I was wondering, how can I use this briefly after using it, thinking about the old days when I was able to connect and felt so extreme. If you need to get to the state , use the useSelector , if you need to dispatch the action , useDispatch , don’t worry about the length of propsType when there are many states or actions that need to connect .
useSelector can also be used with the reselect library, the common redux state selector

I tried writing useDispatch to use instead of react-redux ‘s existing useDispatch and didn’t see any errors at all:

Easy to understand, right, then use it without thinking, note that in order to use the whole combo of hooks as above, your application needs to install react, react-dom v16.8 and above and react-redux. v7.1.0 and above . Also refer to react-redux hooks as well as dig deeper, you can read here . Above is my share on how to use some new hooks of react-redux , thanks for watching.
Demo code link: https://codesandbox.io/s/redux-hooks-rqs76

Share the news now

Source : Viblo