处于Redux状态的数组,React



我被卡住了,找不到如何在Redux State中存储字典。

const productinuse = (state = [], action) => {
switch (action.type) {
case 'productinuse':
return [...state, action.payload];
default:
return state;
}
};
export default productinuse; 
animModal(product) {
console.log(product);
() =>
store.dispatch({
type: 'productinuse',
payload: product,
}),
store.dispatch({type: 'toggleproductmodal'});
}

我试图存储在上面的词典示例:

{"UNIT": "Kg", "images": ["https://example.com/image.jpg"], "module_id": "1", "product_id": "6", "product_img": "example.com/image2.js", "product_name": "Ananas", "product_prix": "8.5", "product_text": "1 Kg Ananas."}

我总是得到默认的空数组作为一个值。

编辑

const initialState = {
productinuse: [],
};
const productinuse = (state = initialState, action) => {
console.log(action.payload + 'DEBUGGINS');
switch (action.type) {
case 'productinuse':
return Object.assign({}, state, {
productinuse: [...state, action.payload],
});
default:
return state;
}
};
export default productinuse;

**编辑2*

<TouchableOpacity
key={product.product_id}
style={{width: '50%', position: 'relative'}}
onPress={() =>
this.setState(
{
product: product,
},
(product) =>
store.dispatch({
type: 'productinuse',
payload: product,
}),
() => store.dispatch({type: 'toggleproductmodal'})}>

现在,当我执行该操作时,我得到一个TypeError:传播不可迭代实例的无效尝试。为了可迭代,非数组对象必须具有Symbol.iterator方法。

试试这个:

状态:

const initialState = {
productinuse: []
}

减速器:

const productinuse = (state = initialState, action) => {
switch (action.type) {
case 'productinuse':
return Object.assign({}, state, {
productinuse: [...state.productinuse, action.payload],
});
default:
return state;
}
};
export default productinuse; 

相关内容

  • 没有找到相关文章

最新更新