From the course: Build Modern Web Apps with React, Hooks, State Management, and APIs Using Vite or Next.js

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Counter component

Counter component

Welcome back. Now we have to set up quickly our counter component. So let's go inside our app.tsx which doesn't have anything. Over here we first have to declare a count variable and we are going to use selector hook which is from react-redux. Inside that, we have to pass on a function. In that function, we are going to have a state of type root state and this function body is actually going to receive state dot counter dot value. So useSelector hook takes a selector function and the responsibility of that selector function is to actually has the initial state that you are going to have. So because we don't have useState or any other hook here, we have to access the Redux store and that particular piece of state to actually get back what we need. This will give us the initial state when we are going to run our application for the very first time. And we need that variable inside our component because whenever the value for this is going to change, React is going to re-render that on…

Contents