Build a React Application with help of Redux which renders a counter that displays a counter value and can be toggled
- create a
README.mdfile - create a
.gitignorefile to ignorenode_modules - run
npm install - run
npm install redux react-redux - run
npm start
- as the common convention recommends, create a
storefolder to store the Redux related files - inside of it, create a
index.jsfile 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
Providercomponent fromreact-redux - wrap the root component with
<Provider> - import
storefromstore/index - set the
storeprop on the<Provider>component tostore
- utilize this Provider store in the
Counter.jscomponent - get access to the Redux store into the data to output the current counter value
- import the React Redux's
useSelectorcustom hook - use
useSelector()inside of theCounterfunction
- import the React Redux's
- output the
countervalue
- 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
Counterclass inCounter.js& extend it toComponent - render the JSX code inside of it
- add all the relevant methods
- add the
thiskeyword to refer to these methods - get access to Redux with help of the
connectfeature
- add a new
buttonto increase the counter by 5 (or any other number) inCounter.js - add a new
action.type === increaseinstore/index.js& set anaction.amount - add a new
increaseHandlerfunction inCounter.js
- add a new state to the Redux store to show/display the counter
- dispatch the
toggleaction inside of thetoggleCounterHandlerinCounter.js - get access to this
togglestate with help ofuseSelector()inside of theCountercomponent & store it in ashowconstant - render the counter conditionally by using this
showconstant
- run
npm install @reduxjs/toolkit react-redux - in
store/index.js, importcreateSlicefrom@reduxjs/toolkit - use
createSlice()
- get rid of
counterReducerinstore/index.js - save the
createSlicevalue in a newcounterSliceconstant - register this
counterSliceto the store by calling thereducermethod on it & passing it as an argument ofcreateStore - use
configureStoreinstead ofcreateStore& pass a configurable object to it
- get hold of the
counterSlice's action identifiers for dispatching actions & export it - import
counterActionsinCounter.js - use
counterActionsto dispatch actions
- in
App.js, add theHeader&Authcomponents - add a brand new
authSlicefor the authentication state with help ofcreateSlice()instore/index - use the
authActionsin the different components to dispatch actions
- render conditionally the
Auth&UserProfilecomponents inApp.js - render conditionally the navigation in
Header.js - dispatch the login action when the
Loginbutton is clicked inAuth.js - dispatch the logout action when the
Logoutbutton is clicked inHeader.js
- add a new
counter.jsfile in thestorefolder - add a new
auth.jsfile in thestorefolder - in
store/index.js, create a main store & merge all the slice reducers together