Build a React Application with help of Redux which renders a counter that displays a counter value and can be toggled
- create a
README.md
file - create a
.gitignore
file to ignorenode_modules
- run
npm install
- run
npm install redux react-redux
- run
npm start
- as the common convention recommends, create a
store
folder to store the Redux related files - inside of it, create a
index.js
file in which you will create a store & a reducer - connect your React app to this Redux store so that the components of this app can dispatch and listen
- export the store as a default
- provide this store to the React app
- go to the
src/index.js
- import the
Provider
component fromreact-redux
- wrap the root component with
<Provider>
- import
store
fromstore/index
- set the
store
prop on the<Provider>
component tostore
- utilize this Provider store in the
Counter.js
component - get access to the Redux store into the data to output the current counter value
- import the React Redux's
useSelector
custom hook - use
useSelector()
inside of theCounter
function
- import the React Redux's
- output the
counter
value
- in
Counter.js
, add 2 buttons to increment & decrement the counter - use
useDispatch()
to dispatch actions & wire up these buttons with help of 2 functions
- add a new
Counter
class inCounter.js
& extend it toComponent
- render the JSX code inside of it
- add all the relevant methods
- add the
this
keyword to refer to these methods - get access to Redux with help of the
connect
feature
- add a new
button
to increase the counter by 5 (or any other number) inCounter.js
- add a new
action.type === increase
instore/index.js
& set anaction.amount
- add a new
increaseHandler
function inCounter.js
- add a new state to the Redux store to show/display the counter
- dispatch the
toggle
action inside of thetoggleCounterHandler
inCounter.js
- get access to this
toggle
state with help ofuseSelector()
inside of theCounter
component & store it in ashow
constant - render the counter conditionally by using this
show
constant
- run
npm install @reduxjs/toolkit react-redux
- in
store/index.js
, importcreateSlice
from@reduxjs/toolkit
- use
createSlice()
- get rid of
counterReducer
instore/index.js
- save the
createSlice
value in a newcounterSlice
constant - register this
counterSlice
to the store by calling thereducer
method on it & passing it as an argument ofcreateStore
- use
configureStore
instead ofcreateStore
& pass a configurable object to it
- get hold of the
counterSlice
's action identifiers for dispatching actions & export it - import
counterActions
inCounter.js
- use
counterActions
to dispatch actions
- in
App.js
, add theHeader
&Auth
components - add a brand new
authSlice
for the authentication state with help ofcreateSlice()
instore/index
- use the
authActions
in the different components to dispatch actions
- render conditionally the
Auth
&UserProfile
components inApp.js
- render conditionally the navigation in
Header.js
- dispatch the login action when the
Login
button is clicked inAuth.js
- dispatch the logout action when the
Logout
button is clicked inHeader.js
- add a new
counter.js
file in thestore
folder - add a new
auth.js
file in thestore
folder - in
store/index.js
, create a main store & merge all the slice reducers together