Answer the question
In order to leave comments, you need to log in
How to do actions after State redux is updated?
Good afternoon, working with Redux, I have already encountered this problem 2 times. How to make actions in JS only after the State has been updated. For example, there is a reducer for a cart.
The code is like this
import {
ADD_TO_CART,
ADD_QUANTITY,
SUB_QUANTITY,
REMOVE_FROM_CART,
EMPTY_CART,
UPDATE_PRICE
} from '../actions/actions-types/aside-actions'
let cartInStorage = localStorage.getItem('cart')
let cartInStorageObject = JSON.parse(cartInStorage)
const initialState = {
cart: cartInStorageObject || [],
totalPrice: 0
}
export default function asideReducer(state = initialState, action) {
let cart = state.cart
let cartStringify = JSON.stringify(cart)
localStorage.setItem('cart', cartStringify)
switch (action.type) {
case ADD_TO_CART:
let itemInCart = cart.find(item => item.product._id == action.payload.product._id)
return{
...state // Example return
}
default:
return state
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question