首页 > 解决方案 > 如何在本机反应中将对象从函数传递到组件

问题描述

我有一个使用“FlatList”创建的对象列表,当我单击一个元素时,我会通过“导航”转到一个页面并显示值。在 Ordine 组件中,我尝试打印,但它返回错误。

export default class ItemComponent extends Component {
    //handling onPress action  
    getListViewItem = (item) => {
        this.props.navigation.navigate('Ordine', { otherParam: 'Ordine' });
        return (
            <Ordine item={item} />
        );
    }
    render() {
        return (
            <ScrollView showsVerticalScrollIndicator={false} contentContainerStyle={{
                flexGrow:
                    1, centerContent: true, paddingBottom: Constants.statusBarHeight
            }}>
                <FlatList
                    data={this.props.items}
                    keyExtractor={(item, index) => index.toString()}
                    renderItem={({ item }) => <TouchableOpacity onPress={this.getListViewItem.bind(this,
                        item, this.props.navigation)} activeOpacity={1} >
                        <Item item={item} />
                    </TouchableOpacity>
                    }
                    ItemSeparatorComponent={this.renderSeparator}
                />
            </ScrollView>
        );
    }
}

排序组件

export default class Ordine extends Component {

    render() {
        return (
            <View
                style={{
                    flexDirection: 'row',
                    height: 100,
                    padding: 20,
                }}>
                <Text>{this.props.item.Nome}</Text>
                <Text>text</Text>
            </View>
        );
    }
}

当我单击一个元素时,它返回错误:TypeError: undefined is not an object (evalating 'this.props.item.Nome')

标签: javascriptreactjsreact-native

解决方案


推荐阅读