【问题标题】:Display Json data with multiple levels显示多层次的 Json 数据
【发布时间】:2021-12-17 12:15:27
【问题描述】:

我正在尝试实现 add_to_cart redux,但它一直给我错误:TypeError: Invalid attempt to spread non-iterable instance。 为了可迭代,非数组对象必须有一个 Symbol.iterator 方法。

不确定我可以更改什么以使其可迭代,因为我只是传入对象

减速机:

import { ADD_TO_CART } from '../constants'

const initialState = {
    cardData: [],
    totalPrice :0
}
export default function cardItems(state = initialState, action) {
    switch (action.type) {
        case ADD_TO_CART:
            return [
                ...state,
                {cardData: action.data}
            ]
        default:
            return state
    }


}

rootreducer:

import {combineReducers} from 'redux'
import cardItems from './reducer'

export default combineReducers({
    cardItems,
})

主页:

renderRow =({item}) =>{
    return(
      <TouchableHighlight >
    
        <View style={styles.row}>
        <Icon name="add" size ={20} style= {styles.addToCartBtn} onPress ={()=> {this.props.addToCartHandler(item.price)}} />
        
        </View>
      </View>
       </TouchableHighlight>
    )
  }

当我在控制台登录时,似乎只传递了商品的价格,那么为什么会出现此错误

【问题讨论】:

    标签: react-native view format rendering display


    【解决方案1】:

    你的初始状态是一个对象

    const initialState = {
        cardData: [],
        totalPrice :0
    }
    

    那么你应该像这样返回一个对象:

    return {
                ...state,
                {cardData: action.data}
            }
    

    【讨论】:

      猜你喜欢
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      相关资源
      最近更新 更多