【问题标题】:React-Native StackNavigatorReact-Native StackNavigator
【发布时间】:2017-05-20 20:45:20
【问题描述】:

好的,伙计们,我有一个问题,我是 react-native 开发的新手,并且遇到了 StackNavigator 的问题。当我按下一个用于导航当前屏幕的按钮时,它给了我一个像这样的错误: enter image description here

这是我正在使用的代码,我无法让它工作,

   static navigationOptions = {
     title: 'Welcome',
     };

    <Button
      onPress={() => navigate('News')}
      title="News"
    />

    const NewsApp = StackNavigator({
    Home: { screen: Splash },
    News: { screen: News }
    });

【问题讨论】:

    标签: javascript react-native navigation react-redux


    【解决方案1】:

    如果您确实尝试从创建它的组件中导航,您将无法访问导航器,因为它只会将导航注入到 NewsApp 中的任何内容。

    您应该在导航堆栈的最顶层组件内处理后按。但是,如果您需要控制诸如您所拥有的地方的导航,那么实施 redux 可能对您来说是一个很好的解决方案。

    【讨论】:

      【解决方案2】:

      在渲染函数中,你必须像这样提取导航器:

      render()
      {
        const { navigate } = this.props.navigation;
        return (
          <Button
            onPress={() => navigate('News')}
            title="News"
          />
        )
      }
      

      开始使用堆栈导航:

      //First Screen
      
            import {
            StackNavigator,
          } from 'react-navigation';
      //Added screens to navigate.    
          const BasicApp = StackNavigator({
            Main: {screen: MainScreen},
            Profile: {screen: ProfileScreen},
          });
      //Second screen (Main Screen)
      //This is a welcome screen which appears first in the route declared in the previous code
          class MainScreen extends React.Component {
            static navigationOptions = {
              title: 'Welcome',
            };
            render() {
            //constant for navigation 
              const { navigate } = this.props.navigation;
              return (
                <Button
                  title="Go to Jane's profile"
                  onPress={() =>
                  navigate('Profile', { name: 'Jane' })//Navigate to Profile with name
                  }
                />
              );
            }
          }
      
      //Third screen (Profile screen)
      
          class ProfileScreen extends React.Component {
            static navigationOptions = ({navigation}) => ({
              title: navigation.state.params.name, //extracted name from the params
            });
            render() {
              const { goBack } = this.props.navigation;//used to goBack prop
              return (
                <Button
                  title="Go back"
                  onPress={() => goBack()} 
                />
              );
            }
          }
      

      干杯:)

      【讨论】:

      • 这是第一部分 export default class Splash extends Component { static navigationOptions = { title: 'Welcome', };渲染() { 常量 { 导航 } = 'this.props.navigation'; return ( 新闻 ); } }
      • 这是第二部分 class Chat extends React.Component { static navigationOptions = { title: 'Chat with Lucy', };渲染() { 常量 { 导航 } = 'this.props.navigation'; return ( Chat with Lucy ); } } const NewsApp = StackNavigator({ Home: { screen: Splash }, News: { screen: Chat } });
      • 你为什么把 const { navigate } = 'this.props.navigation' 用引号括起来?删除引号并再次运行代码
      • 在我删除引号后它说未定义不是一个对象(评估'this.props.navigation.navigate')
      • 请问,您能否将上述代码添加到问题中并解释一下您的应用程序在单击哪个按钮时会崩溃?
      猜你喜欢
      • 2019-05-08
      • 1970-01-01
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      相关资源
      最近更新 更多