Home > OS >  how action contacted the reducer to change the state of the object
how action contacted the reducer to change the state of the object

Time:03-20

I am sorry it might be a silly question but i want to know i have redux store first let me explain the data flow there The data is getting store at a global level state which i have in my store.js which have been declare in my productReducer.js There i define a switch statement and there i alter the state of the product

productReducer.js

my code here

import { 
    PRODUCT_LIST_SUCCESS, 
    PRODUCT_LIST_REQUEST,
    PRODUCT_LIST_FAIL,
    CLEAR_ERROR
} from '../constants/productConst'

export const productReducer = (state  ={product:[]},action)=>{
    switch (action.type) {
        case PRODUCT_LIST_REQUEST:
            return{
                laoding: true,
                product:[],
            }
        case PRODUCT_LIST_SUCCESS:
            return{
                laoding: false,
                product:action.payload.products,
                productsCount:action.payload.productCount,
            }
        case PRODUCT_LIST_FAIL:
            return{
                laoding: false,
                error:action.payload,
            }
        case CLEAR_ERROR:
            return{
                ...state,
                error:null
            }
        default:
            return {
                ...state,
            }
    }
}

i have action productAction.js

import axios from 'axios'


import { 
    PRODUCT_LIST_SUCCESS, 
    PRODUCT_LIST_REQUEST,
    PRODUCT_LIST_FAIL,
    CLEAR_ERROR
} from '../constants/productConst'

export const getProduct = () => async (dispatch) =>{
    console.log("Been executed at the action")
    try {
        dispatch({type:PRODUCT_LIST_REQUEST})
        const {data} = await axios.get("api/v1/products")
        dispatch({
            type:PRODUCT_LIST_SUCCESS,
            payload:data,
        })
    } catch (error) {
        dispatch({
            type:PRODUCT_LIST_FAIL,
            payload:error.response.data.message,
        })
    }
}

export const clearError =() =>async (dispatch)=>{
    dispatch({type:CLEAR_ERROR})
}

Let me Sum up my question when i neeed to update the state from the frontend i call the action but there is no way the action and the reducer connected together how the state of the product is altered in my case

CodePudding user response:

To answer your question "how do actions contact the reducer to change the state of the object?":

The overall redux store setup enables this, especially how you registered your productReducer.

Let's go through the typical flow to illustrate how everything is connected:

  1. Somewhere in a React component, a user-interaction (e.g. button click) or something automatic dispatches the async getProduct() action. This is possible because getProduct is either a prop of the component (Redux' connect API) or you're using the useDispatch hook.
  2. The store setup knows that PRODUCT_LIST_SUCCESS is handled by your productReducer. We go through the switch statement and now state.product.product holds an array of products (careful with the naming there btw, plural vs. singular).
  3. Any React component interested in state.product.product is now notified that the state has changed. They're getting notified because either the connect API (mapStateToProps) or useSelector connects the state with the mounted React component. The products can now be (re-)rendered, or clicked on, etc..

CodePudding user response:

Action

An action is a plain JavaScript object that has a type field. You can think of an action as an event that describes something that happened in the application.

Reducer

You can think of a reducer as an event listener which handles events based on the received action (event) type.

By using dispatch() you are dispatching the event and then comes the following in the reducer logic:

  • Check to see if the reducer cares about this action
    • If so, make a copy of the state, update the copy with new values, and return it
  • Otherwise, return the existing state unchanged

If you are interested about more then please have a look into official redux documentation, there is really everything you will need.

  • Related