【问题标题】:How to make a navigation from an item of FlatList in React Native?如何从 React Native 中的 FlatList 项进行导航?
【发布时间】:2018-02-13 11:30:05
【问题描述】:

我现在正在使用 React Native 开发一个 APP,我想从 FlatList 的每个项目中进行导航。

例如,使用create-react-native-app。在 App.js 中,代码如下:

export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <Text>Open up App.js to start working on your app!</Text>
        <Text>Changes you make will automatically reload.</Text>
        <Text>Shake your phone to open the developer menu.</Text>
        <FlatList
          data={[{key: 'a'}, {key: 'b'}]}
          renderItem={({item}) => <Text>{item.key}</Text>}
        />
      </View>
    );
  }
}

当我单击 FlatList 的一个项目时,我想导航到另一个组件,我的代码如下:

export default class App extends React.Component {
  cb() {
    this.props.navigator.push({
      component: QuestionDetail
    });
  }
  render() {
    return (
      <View style={styles.container}>
        <Text>Open up App.js to start working on your app!</Text>
        <Text>Changes you make will automatically reload.</Text>
        <Text>Shake your phone to open the developer menu.</Text>
        <FlatList
          data={[{key: 'a'}, {key: 'b'}]}
          renderItem={({item}) => <Text onPress={this.cb.bind(this)}>{item.key}</Text>}
        />
      </View>
    );
  }
}

但是有一个错误。

任何人都可以从 FlatList 的项目中制作导航器吗?事实上,FlatList 的每一项在被点击时都应该成为一个导航器。

谢谢!

【问题讨论】:

  • 你在使用 react-navigation 吗?
  • 不,我不知道怎么用。其实我是想用FlatList渲染一个列表,当点击每个item时,页面会跳转到另一个组件

标签: javascript reactjs react-native


【解决方案1】:

使用 react-navigation(如果您想使用 react-native-navigation,流程非常相似):

导入 StackNavigator:

imports...
import { Text, View, Button, FlatList, TouchableOpacity } from 'react-native';
import { StackNavigator } from 'react-navigation';

创建您的屏幕/容器堆栈:

class QuestionDetailScreen extends React.Component {
  render() {
    return (
      <View>
        <Text>QuestionDetail Screen</Text>
        <Button
          title="Go to List"
          onPress={() => this.props.navigation.navigate('List')}
        />
      </View>
    );
  }
}

在您的列表中:

class ListScreen extends React.Component {

  cb = () => {
    this.props.navigation.push('QuestionDetail');
  }
  render() {
    return (
      <View>
        <Text>Open up App.js to start working on your app!</Text>
        <Text>Changes you make will automatically reload.</Text>
        <Text>Shake your phone to open the developer menu.</Text>
        <FlatList
          data={[{key: 'a'}, {key: 'b'}]}
          renderItem={({item}) => 
             <TouchableOpacity onPress={() => this.cb()}> 
                 <Text>{item.key}</Text> 
             </TouchableOpacity>}
        />
      </View>
    );
  }
}

最后导出你的 stacknavigator:

const RootStack = StackNavigator(
  {
    List: {
      screen: ListScreen,
    },
    QuestionDetail: {
      screen: QuestionDetailScreen,
    },
  },
  {
    initialRouteName: 'List',
  }
);

export default class App extends React.Component {
  render() {
    return <RootStack />;
  }
}

【讨论】:

  • 感谢您的回复。事实上,我的错误是“未定义不是一个对象(评估'this.props.navigator.push”我认为你的代码会导致同样的错误?
  • 但是你用的是什么?反应导航?还是哪一个?知道我们可以准确地为您提供帮助,如果您不确定,请尝试将此代码放在您的 App.js 上,也许它会有所帮助。
  • 我的App.js在上面,其实我是用create-react-native-app作为模板,加了一点demo的代码。我没用react navigation之类的,其实我在别人的App代码里看到this.props.navigator.push({component: QuestionDetail});这样的代码,它的功能是跳转到另一个组件,但是遇到错误,别人说我应该加一些代码像{...props} 到根元素,但它也不起作用。我是 React Native 的新手,所以我所描述的可能真的很混乱。对此感到抱歉
  • 我的目标真的很简单,使用FlatList,当我点击FlatList的每一项时,我可以导航到另一个组件,这就是我想要的
  • 我认为最简单的方法取决于你,但我喜欢和目前使用的是react-native-navigation,因为我使用redux,我觉得它让事情变得更容易,而且我觉得它在性能和稳定性,但正如我所说,这完全取决于您,我回答中的示例是使用 react-navigation 完成的,但我认为 RNN 更好更容易。
【解决方案2】:
export default class App extends React.Component {
  cb() {
    this.props.navigator.push({
      component: QuestionDetail
    });
  }
  render() {
    return (
      <View style={styles.container}>
        <Text>Open up App.js to start working on your app!</Text>
        <Text>Changes you make will automatically reload.</Text>
        <Text>Shake your phone to open the developer menu.</Text>
        <FlatList
          data={[{key: 'a'}, {key: 'b'}]}
          renderItem={({item}) => 
                <TouchableOpacity onPress={() => this.cb()}> 
                    <Text>{item.key}</Text> 
                </TouchableOpacity>}
        />
      </View>
    );
  }
}

您可以使用箭头函数调用导航函数,即cb()。 我使用了TouchableOpacity 来赋予文本元素onPress 功能。

【讨论】:

  • 谢谢,我测试你的代码,同样的错误,undefined is not an object(评估'this.props.navigator.push
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多