36
loading...
This website collects cookies to deliver better user experience
Store.
This provides consistency to the application and makes testing easy.Action, Reducer, Store and View
. And believe me, they work in the same way as their name suggests.task_Array
that the reducer will determine. It returns the new state and tells the store how to do it. Simply, it uses the action it receives to determine the change.countValue
and darkMode
enabling.redux-boilerplate
and open it in the VSCode. Now, open the terminal and create the react app.npx create-react-app redux-boilerplate
Note: You’ll need to have Node >= 14.0.0 and npm >= 5.6 on your machine.
<link>
and <script>
from both the links and paste it to the public>>index.html file at appropriate place.ProTip:To check bootstrap added successfully, just simply paste the code to App.js
file and see red colored font.
import './App.css';
function App() {
return (
<>
<h1 className="text-danger"> Hello World!</h1>
</>
);
}
export default App;
src
folder. Now create a file Navbar.js
& another file Shop.js
both in components folder.import React from "react";
const Navbar = () => {
return (
<div>
<nav className="navbar navbar-expand-lg navbar-dark bg-dark">
<div className="container-fluid">
<a className="navbar-brand" href="/">
Redux BoilerPlate
</a>
<button
className="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav me-auto mb-2 mb-lg-0">
<li className="nav-item">
<a className="nav-link active" aria-current="page" href="/">
Home
</a>
</li>
<li className="nav-item">
<a className="nav-link" href="/about">
About
</a>
</li>
</ul>
</div>
<div>
<div className="form-check form-switch mx-4">
<input className="form-check-input" type="checkbox" id="flexSwitchCheckDefault" />
<label className="form-check-label text-light" htmlFor="flexSwitchCheckDefault">Darkmode</label>
</div>
</div>
<div>
<button className="btn btn-info mx-1">CountValue : 0 </button>
</div>
</div>
</nav>
</div>
);
};
export default Navbar;
import React from 'react';
const Shop = () => {
return (
<>
<div className="container p-3">
<div className={`alert alert-success` } role="alert">
<h2>Redux BoilerPlate</h2>
</div>
<h3 className="my-5">Simple Starter template for redux-store</h3>
<div className="container my-5">
<h1 className="display-1">This is LightMode</h1>
</div>
<div className="container d-flex">
<button className="btn-lg btn-success mx-4" > + </button>
<h1 className="display-6">You Count Value = 0 </h1>
<button className="btn-lg btn-danger mx-4" > - </button>
</div>
</div>
</>
)
}
export default Shop;
import Navbar from './components/Navbar';
import Shop from './components/Shop';
.
.
.
return (
<>
<Navbar />
<div className="container p-3">
<Shop />
</div>
</>
);
}
npm i redux
npm i react-redux
npm i redux-thunk
Get more info here .
countValue
and enable darkMode
.As discussed, action-creators create the action to change the state variable and reducer is a pure function that takes action & the previous state of the application and returns the new state.
state
inside the src
folder. Inside the src
folder, we create two more folders :index.js
file in action-creators
folder and write code for action-creator. We will create action here.export const increaseCount = (countValue) => {
return (dispatch) => {
dispatch ({
type: 'increase',
payload: countValue
})
}
}
dispatch
which dispatches type & payload. decreaseCount
, enableDarkMode
and enableLightMode
in the same file.state/action-creators/index.js
from here.Reducers
. Reducers need not to be a single one, for the different states, we have different reducers. Like here, we'll create two reducers file named countReducer.js
and darkmodeReducer.js
inside reducer
folder, which will look after the countValue
and darkMode
state variables.countReducer.js
to change the count value.const reducer = (state=0, action) => {
if(action.type === 'increase') {
return state + action.payload;
}
else if(action.type === 'decrease') {
return state - action.payload
}
else {
return state;
}
}
export default reducer;
darkModeReducer.js
by own first and compare your results from below.const reducer = (state= 'OFF', action) => {
if(action.type === 'darkmode') {
state = action.payload;
}
else if(action.type === 'lightmode') {
state = action.payload;
}
return state;
}
export default reducer;
combineReducers
, which combines all the reducer functions and export them as a single reducer function.index.js
in reducer
folder and paste the below code.import { combineReducers } from "redux";
import countValueReducer from './countReducer';
import darkmodeReducer from './darkmodeReducer';
//combinnig all the reducers here
const reducer = combineReducers({
countValue: countValueReducer,
darkmode: darkmodeReducer
})
export default reducer;
store.js
inside the state
directory. Use to code below for store.js
.import { applyMiddleware, createStore } from "redux";
import thunk from "redux-thunk";
import reducer from "./reducer";
export const store = createStore(reducer,{}, applyMiddleware(thunk));
createStore : It takes the reducers, preloaded state and enhancer function(optional) to create the store. It returns an object that holds the complete state of your app.
applyMiddleware : Returns a store enhancer that applies the given middleware.
redux-thunk: Helps to execute asynchronous functions.
src/index.js
(main index.js file) and import the store here. The file will look likeimport React from 'react';
.
.
.
import { Provider } from 'react-redux';
import { store } from './state/store';
ReactDOM.render(
.
.
<Provider store={store}>
<App />
</Provider>
.
.
);
export * as actionCreators from './action-creators/index';
+
/ -
button and darkmode
toggle, but now we got access to the state variables through store, we can use them to display their values on the UI.import { useSelector } from "react-redux";
.
.
const countValue = useSelector((state) => state.countValue);
const darkmode = useSelector((state) => state.darkmode);
.
.
0 to {countValue}
, like this<button className="btn btn-info mx-1">CountValue : {countValue}</button>
darkMode
state in App.js
and Shop.js
.import { useSelector } from "react-redux";
import './App.css';
import Navbar from './components/Navbar';
import Shop from './components/Shop';
function App() {
const darkmode = useSelector(state => state.darkmode);
let darkmodeStyle = {
backgroundColor:'#2d2d2d',
color:'whitesmoke'
}
return (
<>
<Navbar />
<div className="container p-3" style={darkmodeStyle}>
<Shop />
</div>
</>
);
}
export default App;
shop.js
, just simply import the useSelector
hook like in Navbar.js
usingimport { useSelector } from "react-redux";
Shop
component..
.
const darkmode = useSelector((state) => state.darkmode);
.
.
useDispatch
hook, which dispatches the action-creators and bindActionCreators
to bind all action creators.Navbar.js
and Shop.js
, import them asimport { useSelector, useDispatch } from "react-redux";
import { bindActionCreators } from 'redux';
import { actionCreators } from '../state/index';
Navbar.js
, we use enableDarkMode
action-creator to dispatch.const dispatch = useDispatch();//to dispacth the action creator functions
const {enableDarkMode} = bindActionCreators(actionCreators, dispatch);
darkMode
functionality to toggle button present in navbar like this<input className="form-check-input" onClick={()=>{darkmode === 'OFF'?enableDarkMode('ON'):enableDarkMode('OFF')}} type="checkbox" id="flexSwitchCheckDefault" />
App.js
as.
.
const darkmode = useSelector(state => state.darkmode);
let darkmodeStyle = {}
if(darkmode === 'ON') {
darkmodeStyle = {
backgroundColor:'#2d2d2d',
color:'whitesmoke'
}
}
return (
<>
.
.
Shop.js
to switch the title on the main page from LightMode
to DarkMode
.<div className="container my-5">
<h1 className="display-1">This is {darkmode === "ON" ? "DarkMode" : "LightMode"}</h1>
</div>
countValue
action-creator to dispatch, we need to code in Shop.js
.const dispatch = useDispatch();
const balance = useSelector(state => state.countValue);
const {increaseCount, decreaseCount} = bindActionCreators(actionCreators,dispatch);
increaseCount/decreaseCount
functionality to + and - buttons present in screen like this.<div className="container d-flex">
<button className="btn-lg btn-success mx-4" onClick={()=>(increaseCount(1))}> + </button>
<h1 className="display-6">You Count Value = {balance} </h1>
<button className="btn-lg btn-danger mx-4" onClick={()=>(decreaseCount(1))}> - </button>
</div>