代码之家  ›  专栏  ›  技术社区  ›  Salman

将嵌套数组项添加到Redux存储时未定义this.props.onPress(行)

  •  0
  • Salman  · 技术社区  · 7 年前

    我有我的 React-Native 正在运行的应用程序 Redux 我正在尝试将嵌套数组项(产品)添加到我的存储中。

    屏幕应该如何工作?

    1. 数据来自 productScreen 是从上一个 屏幕(所选产品)
    2. 循环遍历数据,找到嵌套数组,并将其显示在 FlatList

    什么工作?

    1. 数据来自 是从上一个 屏幕(所选产品)
    2. 扁平列表

    不起作用的是:

    1. 将所选产品添加/删除到购物车(redux)

    当我导航到屏幕时,我得到的错误是this.props.onPress(行)不是一个函数。此.props.onPress(行)未定义。

    我试着在网上和官方网站上寻找解决方案 Redux

    有人知道如何将嵌套数组添加到存储中吗?

    扁平列表

            <FlatList
            style={styles.listContainer}
            data={this.state.filteredProducts}
            renderItem={this.renderItem}
            keyExtractor={(item, index) => index.toString()}
            />
    

    伦德雷特

    renderItem = ({item}) => {
        let items = [];
        if( item.products) {
          items = item.products.map(row => {
            return (<View key={row.id} style={styles.products}>
                <View style={styles.iconContainer}> 
                    <Icon name={row.icon} color="#DD016B" size={25} />
                </View>
                <View style={styles.text}>
                    <Text style={styles.name}>
                        {row.name}
    
                    </Text>
                    <Text style={styles.price}>
                    € {row.price}
                    </Text>
                </View>
                {console.log('renderItem',this.state.row)}
                <View style={styles.buttonContainer}  onPress={this.props.addItemToCart} >
                    <TouchableOpacity onPress={this.props.onPress(row)} > 
                       <Icon style={styles.button} name="ios-add" color="white" size={25} />
                    </TouchableOpacity>
                </View>
                <View style={styles.buttonContainer}  onPress={this.props.removeItem} >
                    <TouchableOpacity onPress={() => this.props.onPress(row)} > 
                        <Icon style={styles.button} name="ios-remove" color="white" size={25} />
                    </TouchableOpacity> 
                </View>
            </View>)
    
          })
        }
    

    向商店添加产品

        const mapDispatchToProps = (dispatch) =>{
    
        return{
            addItemToCart:(product) => dispatch({
                type:'ADD_TO_CART', payload: product, qty
    
            }),
    
            removeItem:(product) => dispatch ({
                type:'REMOVE_FROM_CART' , payload: product, qty
            })  
        }
    
    }
    
    export default connect(null, mapDispatchToProps) (ProductScreen);
    

    Store.JS

    import {createStore} from 'redux';
    import cartItems from '../reducers/carItems';
    
    export default store = createStore(cartItems)
    

    cartItems.JS

    const cartItems = (state = [], action) => {
        switch (action.type)
        {
            case 'ADD_TO_CART':
            // console.log('CarItems.JS', action.payload)
                if (state.some(cartItem => cartItem.id === action.payload.id)) {
                    // increase qty if item already exists in cart
                    return state.map(cartItem => (
                        cartItem.id === action.payload.id ? { ...cartItem, qty: cartItem.qty + 1 } : cartItem
    
                        ));
    
                }
                return [...state, { ...action.payload, qty: 1 }]; // else add the new item to cart
    
            case 'REMOVE_FROM_CART':
                return state
                    .map(cartItem => (cartItem.id === action.payload.id ? { ...cartItem, qty: cartItem.qty - 1 } : cartItem))
                    .filter(cartItem => cartItem.qty > 0);
        }
        return state
    } 
    
    export default cartItems 
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Daniel McMillan    7 年前

    看起来您正在将函数调用的结果传递到TouchableOpacity的onPress道具中。

    你可能想试着改变一下 <TouchableOpacity onPress={this.props.onPress(row)} > <TouchableOpacity onPress={() => this.props.onPress(row)} > 正如您在第二个案例中所做的那样。