【问题标题】:this.props.onPress(row) is not defined when adding nested array items to the Redux store向 Redux 存储添加嵌套数组项时未定义 this.props.onPress(row)
【发布时间】:2019-01-30 08:35:02
【问题描述】:

我的React-Native 应用程序与Redux 一起运行,并且正在尝试将嵌套数组项(产品)添加到我的商店。

屏幕应该如何工作?

  1. productScreen 中的数据是从上一个传递过来的 屏幕(选定产品)
  2. 循环遍历数据并找到嵌套数组并将其显示在FlatList
  3. 将所选产品添加/删除到购物车 (redux)

Wat 有效吗?

  1. productScreen 中的数据是从上一个传递过来的 屏幕(选定产品)
  2. 循环遍历数据并找到嵌套数组并将其显示在FlatList

什么不起作用:

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

当我导航到屏幕时,我收到错误 this.props.onPress(row) is not a function。 this.props.onPress(row) 未定义。

我尝试在网上和官方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 

【问题讨论】:

    标签: reactjs redux react-redux


    【解决方案1】:

    看起来您正在将函数调用的结果传递给 TouchableOpacity 的 onPress 属性。

    您可能想尝试更改 &lt;TouchableOpacity onPress={this.props.onPress(row)} &gt;&lt;TouchableOpacity onPress={() =&gt; this.props.onPress(row)} &gt; 就像你在第二种情况下所做的那样。

    【讨论】:

    • 您好丹尼尔,感谢您的回答。当我按下按钮时,它似乎仍然会出现同样的错误。
    • 听起来像 onPress 道具可能是未定义的。您的意思是要调用 addItemToCart/removeItem 吗?请注意,this.props.onPress 指的是屏幕组件上的 prop,而不是 View 组件。
    猜你喜欢
    • 2020-07-01
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多