React Redux with boolean values
so basically i am trying to do when quantity of my cart is >= 1 and if proceed is clicked to make quantity again at 0
export const cartSlice = createSlice({
name: 'cart',
initialState: {
cart: [],
quantity: 0,
isSubmited: false
},
reducers: {
resetCart: (state, action) => {
const itemInCart = state.cart.find((item) => item.id === action.payload);
{console.log(itemInCart)}
if (itemInCart && itemInCart.quantity >= 1 && state.isSubmited === false){
state.isSubmited = action.payload;
console.log(state.isSubmited + 'after dispatch');
}
console.log(state.isSubmited);
},
import classes from "./Proceed.module.css";
import { useSelector,useDispatch } from "react-redux";
import {resetCart} from '../Features/cartSlice';
import PropTypes from 'prop-types';
const Proceed = ({id}) => {
const dispatch = useDispatch();
const cart = useSelector((state) => state.cart.isSubmited);
const onClickHandler = (e) => {
e.preventDefault();
alert("Thank you for your purchase ");
dispatch((resetCart(id)));
console.log(cart);
};
return (
<div className={classes.container}>
<button onClick={onClickHandler} className={classes.btn}>
Proceed to Checkout!
</button>
</div>
);
};
export default Proceed;
Proceed.propTypes = {
id: PropTypes.string.isRequired,
}
1 Reply
When i log out to console it is still false
is it possible to do this thing with redux
cuz it is way easier i think
because you dont have to do like prop drilling and so on
??7