简体   繁体   English

React Redux删除数组对象

[英]React Redux remove array object

I started using redux a short time ago and I'm having a big problem, I'm trying to remove an object from an array but everytime i do that the map function doesn't work, any tips? 我不久前开始使用redux,遇到了一个大问题,我试图从数组中删除一个对象,但是每次我这样做map函数都不起作用时,有任何提示吗?

Reducer: 减速器:

const initialState = {
    lojaState: {
        nome: "Café", cardapio: [{
            setor: 'Salgados', produtos: [
                { nome: 'Coxinha', descricao: 'lalalalal', valor: "3,00"},
                { nome: 'Coxinha', descricao: 'lalalalal', valor: "2,50" }
            ]
        }]
    }
};

export const lojaReducer = (state = initialState, action) => {
    switch (action.type) {
        case CLICK_UPDATE_VALUE:
            return state;
        case CLICK_DELETE_VALUE:
            return [
                ...state.lojaState.cardapio[action.carIndex].produtos.slice(0, action.proIndex),
                ...state.lojaState.cardapio[action.carIndex].produtos.slice(action.proIndex + 1)
            ]
        default:
            return state;
    }
}

Action.js Action.js

export const removeProduto = (cardapioIndice, produtoIndice) => ({
type: CLICK_DELETE_VALUE,
proIndex: produtoIndice,
carIndex: cardapioIndice});

Cardapio.js Cardapio.js

 loja.cardapio.map((cardapio, indCardapio) => (
 <Row>
    {   
        cardapio.produtos.map((produto, indProduto) => (
            <Col md={4} sm={6} xs={12}>
                <Thumbnail src={thumbnail}>
                    <h3>Nome: {produto.nome}</h3>
                    <p>Descricao: {produto.descricao}</p>
                    <p>Valor: R$ {produto.valor}</p>
                </Thumbnail>
                <Button bsStyle="danger" onClick={() => removeProduto(indCardapio,indProduto)}>Delete</Button>
            </Col> 
        ))
    }
</Row>
))

In a Redux reducer, you need to return the full new state . 在Redux减速器中,您需要返回全新状态

Here, you have a state looking like this: 在这里,您的状态如下所示:

const initialState = {
    lojaState: {
        nome: "Café", cardapio: [{
            setor: 'Salgados', produtos: [
                { nome: 'Coxinha', descricao: 'lalalalal', valor: "3,00"},
                { nome: 'Coxinha', descricao: 'lalalalal', valor: "2,50" }
            ]
        }]
    }
};

But your return an array like so: 但是您返回的数组是这样的:

case CLICK_DELETE_VALUE:
    return [
        ...state.lojaState.cardapio[action.carIndex].produtos.slice(0, action.proIndex),
        ...state.lojaState.cardapio[action.carIndex].produtos.slice(action.proIndex + 1)
    ]

The new state is now the array and doesn't look like before. 现在,新状态为数组,看起来不再像以前一样。 Hence, your code can't work with this new state. 因此,您的代码无法在此新状态下工作。

You aren't updating the state correctly while handling the CLICK_DELETE_VALUE action. 您在处理CLICK_DELETE_VALUE操作时未正确更新状态。 Its structure is changed. 其结构已更改。 Update it like 像这样更新

case CLICK_DELETE_VALUE:
            return  {
                ...state,
                lojaState: {
                    ...state.lojaState, 
                    cardapio: state.lojaState.cardapio.map((item, index) => {
                          if (index === action.carIndex) {
                               return {
                                  ...item,
                                  productos: [...item.productos.slice(0, action.proIndex),  ...item.productos.slice(action.proIndex + 1)]
                               }
                          }
                          return item;
                    })
                }
            }

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

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