arrays - React-redux 状态数组管理

标签 arrays reactjs redux state store

所以我基本上开始学习 Redux 并想创建一个简单的商店应用程序,您可以在其中将手机添加到购物车。我创建了一个状态对象,并在其中创建了一个包含对象的数组,其中包含商店中的项目列表。我想更新 [+] 单击订购的商品数量,但现在不起作用。我已经为此苦苦挣扎了 1 个小时,但仍然看不出问题出在哪里。

Reducer 看起来像这样:

const initialState = {
liked:0,
cart:0,
item: [
    {
        id:1,
        name: 'Iphone 8',
        price: 2000,
        desc: 'The new Iphone 8 available at our store!',
        orderedNum: 0
    },
    {
        id:2,
        name: 'Iphone 6',
        price: 1500,
        desc: 'The new Iphone 6 available at our store!',
        orderedNum: 0
    },
    {
        id:3,
        name: 'Samsung S8',
        price: 2200,
        desc: 'The new Samsung S8 available at our store!',
        orderedNum: 0
    },
    {
        id:4,
        name: 'Xiaomi Mi 6',
        price: 1400,
        desc: 'The new Xiaomi Mi 6 available at our store!',
        orderedNum: 0
    },
    {
        id:5,
        name: 'Pocophone P1',
        price: 2100,
        desc: 'The new Pocophone P1 available at our store!',
        orderedNum: 0
    },
    {
        id:6,
        name: 'Nokia 3310',
        price: 999,
        desc: 'The new Nokia 3310 available at our store!',
        orderedNum: 0
    },
]
}

const reducer = (state = initialState, action) => {
const newState = {...state};

switch(action.type) {
    case 'ADD_NUM':
        return state.item.map((el, index) => {
            if(el.id === action.id ){
                return {
                    ...el,
                    orderedNum: el.orderedNum + 1
                }
            }

            return el;
        })
    default:
        break;
}

return newState;
}

export default reducer;
  • 我有行动:

    const mapStateToProps = state => {
    return {
    item: state.item
    }
    }
    
    const mapDispatchToProps = dispatch => {
    return {
    addNum: () => dispatch ({
      type: 'ADD_NUM',
      id: this.props.id,
     value: 1
    })
    }
    

我已经尝试过不同的方式,但我相信这可能是在 reducer 中嵌套的问题。

有人可以建议吗?

最佳答案

让我们从你的 reducer 开始

const reducer = (state = initialState, action) => {
  switch (action.type) {
    case "ADD_NUM":
      return {
        // destruct and return a new object otherwise react wont update the UI
        ...state,
        item: state.item.map(el =>
          el.id === action.id
            ? { ...el , orderedNum: el.orderedNum + action.value }
            : el
        )
      };
    default:
      return state;
  }
};

mapDispatchToProps

const mapDispatchToProps = dispatch => {
  return {
    // add id to addNum
    addNum: id =>
      dispatch({
        type: "ADD_NUM",
        id,
        value: 1
      })
  };
};

项目组件

const Items = ({ item, addNum }) => (
  item.map(el => (
    <div key={el.id}>
      <h1>{el.name}</h1>
      <h3>{el.price}</h3>
      <h3>{`orderedNum: ${el.orderedNum}`}</h3>
      // add the id to addNum
      <button onClick={() => addNum(el.id)}>+</button>
    </div>
  ))
);

CodeSandBox

关于arrays - React-redux 状态数组管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55800920/

上一篇:r - R中的曲线拟合

下一篇:mysql复制停止

相关文章:

javascript - mobx 存储更新时重新渲染组件

node.js - 操作/ reducer 没有按预期导致重新渲染

javascript - React + Redux 简单路由器 : cannot find listen of undefined

arrays - 在 Perl 中从数组中删除值的最佳方法是什么?

javascript - 获取对象内所有数组的二维数组

javascript - 数组中的子数组

javascript - IE-graphql-SCRIPT5022 : Cannot use undefined "Boolean" from another module or realm

c++ - 什么是指向 x 个整数数组的指针?

javascript - create-react-app 构建不接受样式

reactjs - intl.formatMessage 不起作用-react-intl