在我的名为“reducer_product_list”的减速器中,我有这个数组:
let initialState = [
{name: 'Article 1', price: 5, quantity: 10},
{name: 'Article 2', price: 15, quantity: 8},
{name: 'Article 3', price: 7, quantity: 15},
{name: 'Article 4', price: 9, quantity: 5},
{name: 'Article 5', price: 11, quantity: 100},
{name: 'Article 6', price: 23, quantity: 20},
]
Run Code Online (Sandbox Code Playgroud)
当我得到操作“ADD_TO_CART”时,我想减少所选对象的数量。有效载荷是这些对象之一。
我输入了上面的代码:
export default (state = initialState, action) => {
switch (action.type) {
case 'ADD_TO_CART':
initialState.map((product, i) => {
if (product.name === action.payload.name) {
initialState[i].quantity -= 1
return state;
}
});
default: …Run Code Online (Sandbox Code Playgroud) 我是编码初学者。我正在创建我的平面设计师作品集。我已将所有投资组合内容(缩略图、客户名称、描述...)放入 JSON 文件中名为“投资组合”的数组中。数组中的每一项都是不同的项目。我显示了一个缩略图库,当我点击一个缩略图时,一个带有项目详细信息的模式会打开。
我在我的“投资组合”数组上映射以显示画廊,这是有效的。但是当我打开模态时,它总是显示我数组的最后一项。
import React from 'react';
import Modal from 'react-bootstrap/Modal';
class Portfolio extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
error: null,
isLoaded: false,
items: [],
projectName: "",
show: false
};
this.handleShow = () => {
this.setState({ show: true });
};
this.handleClose = () => {
this.setState({ show: false });
};
}
componentDidMount() {
fetch("https://api.myjson.com/bins/1cbaj5")
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
items: result.portfolio
});
},
(error) => {
this.setState({
isLoaded: true, …Run Code Online (Sandbox Code Playgroud)