Skip to main content

Adding Redux in Simple React Web Application - PART 2 [ Dispatching Actions ]

 Install Packages : npm i redux react-redux


 Step 1 : Create store.js file below App.js 

import { createStore } from "redux";

let counterReducer = (state = { value: 10, name: 'pradeep' }, action) => {
    if (action.type === 'ADD') {
        return { ...state, value: state.value + action.payload }
    }
    if (action.type === 'NAME') {
        return { ...state, name: action.payload }
    }

    return state
}

let store = createStore(counterReducer)

export default store


Step 2 : Add the following Code in index.js File 

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { Provider } from 'react-redux'
import store from './store'

ReactDOM.render(
  <React.StrictMode>
    <Provider store={store}>
      <App />
    </Provider>
  </React.StrictMode>,
  document.getElementById('root')
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();

3. And finally App.js Code

import { useSelector, useDispatch } from 'react-redux'

function App() {
  let dispatch = useDispatch()
  let state = useSelector((state) => state)
  console.log(state)
  return (
    <div>
      Hello Redux ..
      <p>{state.value}</p>
      {state.name}

      <button onClick={() => {
        dispatch({ type: 'ADD', payload: 2 })
      }} >ADD</button>
      <button onClick={() => {
        dispatch({ type: 'NAME', payload: 'hello' })
      }} >ShghgUB</button>
    </div>
  );
}

export default App;




Video Links :  Part 2


 

Comments

Popular posts from this blog

Shopping Cart React.js Project with Explanation✅ [ Simple Ecommerce Website using Reactjs ]

Hello guys in this video we will discuss shopping cart web app.  first we need to install the basic react app using - npx create-react-app app-name The folder structure should be containing three files  in components folder as below : In App.js :  import './App.css' ; import Header from './components/Header' ; import ProductList from './components/ProductList' ; import CartList from './components/CartList' ; import { useState } from 'react' ; function App () {   const [ product , setProduct ] = useState ([     {       url : 'https://rukminim1.flixcart.com/image/300/300/l51d30w0/shoe/z/w/c/10-mrj1914-10-aadi-white-black-red-original-imagft9k9hydnfjp.jpeg?q=70' ,       name : 'TRQ White Shoes' ,       category : 'Shoes' ,       seller : 'AMZ Seller Ghz' ,       price : 1999     },     {       url : 'https://5.imimg.com/data5/KC/PC...

Create A Date Range Picker in React JS (By Saifi Code)

 To Create  A Date Range Picker in React JS , need to install following packages :  npm i antd npm i moment Step 1 : import DatePicker from antd and get RangePicker from this package Step 2 : In the JSX Use the package , you will start get picker ui Example :  To get the values use onChange method and save values in state  Try to console the state to check if the values are updating App.js File : import 'antd/dist/antd.css' ; import { DatePicker } from 'antd' ; import { useState } from 'react' ; import moment from 'moment' ; const { RangePicker } = DatePicker ; function App () {   const [ dates , setDates ] = useState ([])   console . log ( dates )     return (     < div style = { { margin : 20 } } >       < RangePicker         onChange = { ( values ) => {                     setDates ( values . map ( item => { ...

MERN Authentication - Login, Register, Verification Email, Forget Password, User Name & Logout on Navbar

In The React Side :  The folder structure will be : Lets start from App.js  On this file we will be adding routing , install the package react-router-dom : import './App.css' ; import React from 'react' ; import {   BrowserRouter as Router ,   Routes ,   Route ,   Link } from "react-router-dom" ; import Signup from './components/Signup' ; import Signin from './components/Signin' ; import Home from './components/Home' ; import ForgetPassword from './components/ForgetPassword' ; import NewSubmit from './components/NewSubmit' ; function App () {   return (     < div >       < Router >         < Routes >           < Route path = "/signup" element = { < Signup /> } />           < Route path = "/signin" element = { < Signin /> } />           < Route path = "/" e...