代码之家  ›  专栏  ›  技术社区  ›  Somename

无法在ReactNative中使用StackNavigator

  •  1
  • Somename  · 技术社区  · 8 年前

    import React, { Component } from 'react';
    import { Text, View, TouchableOpacity, Alert } from 'react-native';
    
    import { StackNavigator } from 'react-navigation';
    import { connect } from 'react-redux';
    
    var styles = require('./styles');
    
    
    class Page1 extends Component {
      static navigationOptions = {
        header: null
      };
    
    componentDidMount() {      // <== Edited
       setTimeout(function() { 
        const { navigate } = this.props.navigation;
        },4000);
     }
      render() {
    
          const { navigate } = this.props.navigation;  // <= Getting error here
    
          const { fname, lname } = this.props.person;
    
        return (
          <View style={styles.container}>
            <Text style={styles.welcome}>
              From Page 1 - {lname} { fname}
            </Text>
            <TouchableOpacity
              // onPress={() => this.props.navigation.navigate('Page2', { user: 'Lucy' }) }
            >
            <Text style={styles.welcome}> click for Page 2</Text>
            </TouchableOpacity>
          </View>
        );
      }
    }
    
    function mapStateToProps(state) {
      return {
        person:state.person
      }
    }
    
    
    export default connect(mapStateToProps) (Page1);
    

    第3页:

    import React, { Component } from 'react';
    import { Text, View } from 'react-native';
    
    import Page1 from './Page1'
    
    import { Provider, connect } from 'react-redux'
    import configureStore from './store'
    
    const store = configureStore()
    
    export default class Page3 extends Component {
      render() {
        return (
          <Provider store={store}>
            <Page1 /> 
          </Provider>     
        );
      }
    }
    

    const SimpleApp = StackNavigator({
      Page1: { screen: Page1 },
      Page2: { screen: Page2 },
      Page3: { screen: Page3 },
    });
    AppRegistry.registerComponent('my03', () => SimpleApp);
    

    除非我发表评论,否则应用程序运行良好 const { navigate } = this.props.navigation; . 我得到以下错误:

    enter image description here

    未定义不是对象(评估“this.props.navigation.navigate”)

    onPress={() => this.props.navigation.navigate('Page2', { user: 'Lucy' }) } onPress={() => navigation.navigate('Page2', { user: 'Lucy' }) }

    我正在尝试使用 ReactNavigation 没有为它创建减速器,因为我不理解 this part

    非常感谢。

    更新1

    import React, { Component } from 'react';
    import { AppRegistry, Text, View } from 'react-native';
    
    import { StackNavigator } from 'react-navigation';
    
    import Page1 from './src/components/Page1'
    import Page2 from './src/components/Page2'
    import Page3 from './src/components/Page3'
    
    const SimpleApp = StackNavigator({
      Page3: { screen: Page3 },
      Page2: { screen: Page2 },
      Page1: { screen: Page1 },
    });
    
    AppRegistry.registerComponent('my03', () => SimpleApp);
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Mustansir Zia    8 年前

    <Page1 navigation={this.props.navigation} />

    解释——原因是什么 this.props.navigation Page 1 Page 3 而不是你用来初始化 StackNavigator 具有所以这是一个全新的 这与StackNavigator完全不匹配。如果 第1页 将是您的 堆栈导航器 这道具。航行 也不会是未定义的,这让我想到了另一个兴趣点。

    第1页 在…内 第3页 在您的react导航堆栈中?这个想法是在我们的 作为屏幕,而不将它们嵌套在一起,我们使用 this.props.navigation.navigate navigation 道具(因为我假设它首先通过 直接)我们只需将该道具传递给我们的嵌套 组件和中提琴!您现在可以访问 这道具。航行 在您的

    此外,由于您的 第3页 <Provider > <SimpleApp > 代替 <Page1 > 第1页 作为堆栈的起点。然后可以将根组件注册为 AppRegistry.registerComponent('my03', () => Page3);

    最后一条信息是,你可以保持你的redux状态和你的导航彼此完全解耦,所以只有当你绝对确定你需要redux商店中的导航状态时,才使用redux集成。一个既有Redux又有ReactNavigation的项目并不意味着你必须将两者结合起来。它们可以完全分开。

    推荐文章