简体   繁体   English

在按钮上单击选中复选框并将数据添加到购物车中反应 redux?

[英]On button click check the checkbox and add the data into cart in react redux?

In this problem what can I do so that the on clicking the button, both the function add to the cart and select the checkbox executes together?在这个问题中,我该怎么做才能在单击按钮时将 function 添加到购物车和 select 复选框一起执行? In the current scenario add to cart is working when the button is clicked but the checkbox isn't selected.在当前场景中,当单击按钮但未选中复选框时,添加到购物车正在工作。 I removed all the styles so that the actual code is readable我删除了所有 styles 以便实际代码可读

YourItems.js YourItems.js

import React from "react";
import { connect } from "react-redux";
import { addOn } from "./data";
import { addOnhandleChange,addOnSelector} from "./AddOnActions";

const YourItems = ({ addOnhandleChange, addOnSelector, selectedId }) => {
  return (
    <div>
      {addOn.map(({ id, name, img, price, unit }) => {
        return (
          <div key={id}>
            <div>
              <img src={img} alt={name} />
              <p>{name}</p>
              <span>Rs. {price}</span>
              <input
                type="checkbox"
                checked={id === selectedId}
                onChange={() => addOnhandleChange(id)}
              />
            </div>
              <button onClick={() =>addOnSelector({id, name,img,price,unit, })}>
                Add
              </button>
          </div>
        )})}
    </div>
  );
};

const mapStateToProps = (state) => {
  return {
    selectedId: state.addOn.selectedId,
  };
};

export default connect(mapStateToProps, { addOnSelector,addOnhandleChange})(YourItems);

AddOnAction.js AddOnAction.js

export const addOnhandleChange = (id) => (dispatch) => {
  dispatch({
    type: "SELECTED_ID",
    payload: id,
  });
};

export const addOnSelector = ({ id, name, img, price, unit }) => (dispatch) => {
  dispatch({
    type: "ADD_TO_CART",
    payload: { id, name, img, price, unit },
  });
};

reducer.js减速器.js

const initialState = {
  selectedId: "",
};

export default function SelectorReducer(
  state = initialState,
  action
) {
  switch (action.type) {
    case "SELECTED_ID":
      return {
        ...state,
        selectedId: action.payload,
      };

    default:
      return state;
  }
}

data.js数据.js

export const addOn = [
  {
    id: 12654,
    img: "https://images.pexels.com/photos/1132047/pexels-photo-1132047.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
    name: "Banana",
    price: 10,
    unit: 1,
  },
  {
    id: 2256435,
    img: "https://images.pexels.com/photos/1132047/pexels-photo-1132047.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
    name: "Mango",
    price: 20,
    unit: 1,
  },
  {
    id: 3429684,
    img: "https://images.pexels.com/photos/1132047/pexels-photo-1132047.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
    name: "Grape",
    price: 30,
    unit: 1,
  },
];

Add a case for "ADD_TO_CART" action type and use the id packed in action.payload in the reducer."ADD_TO_CART"动作类型添加一个案例,并在reducer中使用action.payload中打包的id

export default function SelectorReducer(
  state = initialState,
  action
) {
  switch (action.type) {
    case "SELECTED_ID":
      return {
        ...state,
        selectedId: action.payload,
      };

    case "ADD_TO_CART":
      return {
        ...state,
        selectedId: action.payload.id, // <-- use the payload.id
      };

    default:
      return state;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM