【问题标题】:React Native Navigation between screens from a FlatList在 FlatList 的屏幕之间反应原生导航
【发布时间】:2018-09-14 08:55:36
【问题描述】:

我想使用 FlatList 中的数据导航到第二个屏幕,但无法实现。这是我的代码

主屏幕:

import React, { Component } from "react";
import { View, Text, StyleSheet, Dimensions } from "react-native";
import Ionicons from "react-native-vector-icons/Ionicons";
var CatalogList = require("./catalogFlatList");
import { createBottomTabNavigator, SafeAreaView } from "react-navigation";

class MainScreen extends React.Component {
    render() {
        return (
            <SafeAreaView style={{ flex: 1, backgroundColor: "#ed6b21" }}>
                <CatalogList />
            </SafeAreaView>
        );
    }
}

class SecondScreen extends React.Component {
    render() {
        const { navigation } = this.props;
        const text = navigation.getParam("text", "ERROR");
        return (
            <View
                style={{
                    flex: 1,
                    justifyContent: "center",
                    alignItems: "center"
                }}
            >
                <Text>
                    PASSED DATA IS!
                    {JSON.stringify(text)}
                </Text>
            </View>
        );
    }
}

export default createBottomTabNavigator({
    .. some icons and colors ..
})

以及绘制主屏幕的 CatalogList 模块

import React, { Component } from "react";
import {
    View,
    Text,
    FlatList,
    ActivityIndicator,
    StyleSheet,
    Dimensions,
    TouchableOpacity
} from "react-native";
import { List, ListItem, SearchBar } from "react-native-elements";

class FlatListDemo extends Component {
    constructor(props) {
        super(props);

        this.state = {
            loading: false,
            data: [],
            error: null,
            refreshing: false,
            noData: false,
            tempData: []
        };
    }

    componentDidMount() {
        this.makeRemoteRequest();
    }

    makeRemoteRequest = () => {
      .. fetch some data from url ..
    };

    render() {
        return (
            <List containerStyle={{ borderTopWidth: 0, borderBottomWidth: 0 }}>
                <FlatList
                    data={this.state.data}
                    renderItem={({ item }) => (
                        <TouchableOpacity onPress={item.onItemPressed}>
                            <ListItem
                                title={`${item.name}`}
                                subtitle={`${item.companyname}`}
                                onPress={() => {
                                    this.props.navigation.navigate("Second", {
                                        text: `${item.name}`
                                    });
                                }}
                                containerStyle={{ borderBottomWidth: 0 }}
                            />
                        </TouchableOpacity>
                    )}
                    keyExtractor={item => item.name}
                />
            </List>
        );
    }
}

export default FlatListDemo;
module.exports = FlatListDemo;

这可以呈现 FlatList,但是在单击 ListItem 时会引发错误

undefined is not an object (evaluating 'this2.props.navigation.navigate')

无法弄清楚这背后的逻辑。我不想将所有代码都写在一个页面中。

【问题讨论】:

  • 您的CatalogList 在您使用的导航堆栈中吗?
  • CatalogList 是另一个文件。不知道我应该如何将它包含在堆栈中
  • 查看示例here,您需要先添加一个导航器,然后在其中注册屏幕。只有注册的屏幕才能访问导航道具。
  • 但是我如何从另一个文件注册屏幕。我的意思是我已经创建了按钮栏堆栈并向其中添加了 3 个屏幕。但是 FlatList 屏幕是另一个文件,所以我是否也应该再次创建和注册堆栈,如果是的话,我是否也不需要为其他屏幕粘贴相同的代码?
  • 没有,只要导出包含FlatList的文件,导入到导航器中注册即可。如果您遇到其他问题,请尝试添加snack

标签: react-native


【解决方案1】:

您需要将导航道具传递给您的Flatlist 组件或使用withNavigator HOC 来访问导航道具

<SafeAreaView style={{ flex: 1, backgroundColor: '#ed6b21' }}>
  <CatalogList navigation={this.props.navigation} />
</SafeAreaView>

【讨论】:

  • 如何传递数据并导航到不在 createBottomTabNavigator 中的屏幕?
  • 查看答案,可以通过类似withNavigator的方式传递
【解决方案2】:
Try this



 <FlatList 
         data={this.state.data}
         renderItem={({ item }) => (
              <TouchableOpacity onPress={() => {this.props.navigation.navigate('Second'{text:`${item.name}`})}}>
        <ListItem 
        title={`${item.name}`}
        subtitle={`${item.companyname}`}
        containerStyle={{ borderBottomWidth: 0 }}
        />

    </TouchableOpacity>
    )}
    keyExtractor={item => item.name}
    />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-18
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多