【问题标题】:React Native: Cannot read property 'push' of undefined - Have NavigatorIOS and ES6 bind(this)React Native:无法读取未定义的属性“推送” - 有 NavigatorIOS 和 ES6 绑定(此)
【发布时间】:2015-07-31 20:44:40
【问题描述】:

我正在开发一个 React Native 应用程序,并尝试在通过 Facebook 成功登录后将用户路由到下一个视图。

问题是我继续收到一条错误消息,指出“无法读取未定义的属性‘推送’。”我已经检查了论坛上的所有相关答案,并确保在我的函数调用中包含 NavigatorIOS 和 bind(this) - 所以这些就是问题所在。

由于我是新手开发人员,因此我很想帮助确定问题所在。

这是错误:

Error: Cannot read property 'push' of undefined
 stack: 
  <unknown>                                              index.ios.bundle:1498
  MessageQueue.__invokeCallback                          index.ios.bundle:7235
  <unknown>                                              index.ios.bundle:7151
  guard                                                  index.ios.bundle:7104
  <unknown>                                              index.ios.bundle:7151
  <unknown>                                              index.ios.bundle:7148
  ReactDefaultBatchingStrategyTransaction.Mixin.perform  index.ios.bundle:6552
  Object.ReactDefaultBatchingStrategy.batchedUpdates     index.ios.bundle:15885
  Object.batchedUpdates                                  index.ios.bundle:5084
 URL: undefined
 line: undefined
 message: Cannot read property 'push' of undefined

=============================

这是我的代码

'use strict';

var React = require('react-native');
var Main = require('./App/Components/Main');
var Icon = require('./node_modules/react-native-vector-icons/FontAwesome');
var FacebookLoginManager = require('NativeModules').FacebookLoginManager;
var {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight,
NavigatorIOS,
Navigator,
} = React;

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
 welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
 instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
    marginTop: 10
 },
 icon: {
    fontSize: 20,
    color: 'white',
    paddingVertical: 5,
    paddingHorizontal: 8,
    borderRadius: 4,
    backgroundColor: '#3b5998',
 },
text: {
    marginLeft: 10,
    color: 'white',
    fontWeight: '600',
},
});

class nomsyapp extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      result: 'Find the Foods that Fit Your Lifestyle',
      loggedIn: false,
    };
  }

  login() {
    FacebookLoginManager.newSession((error, info) => {
      if (error) {
        this.setState({result: error});
      } else {
        this.setState({result: info});
        this.setState({loggedIn: true});
        this.props.navigator.push({
          component: Main,
          title: 'Choose Your Lifestyle',
        });
      }
    });
  }

  render() {
    return (
      <View style={styles.container}>
        <TouchableHighlight onPress={this.login.bind(this)}>
         <Icon name="facebook" style={styles.icon}>
            <Text style={styles.text}>Login with Facebook</Text>
          </Icon>
        </TouchableHighlight>
        <Text style={styles.instructions}>
          {this.state.result}
        </Text>
      </View>
    );
  }
};


AppRegistry.registerComponent('nomsyapp', () => nomsyapp);

=============================

【问题讨论】:

    标签: javascript ios iphone facebook react-native


    【解决方案1】:

    这是您的初始路线所需的格式:

    render: function() {
      return (
        <NavigatorIOS
          initialRoute={{
            component: nomsyapp,
            title: 'Nomsyapp',
            passProps: { /*whatever props you want */ },
          }}
        />
      );
    }
    

    此示例基于Facebook docs

    因此,您应该将所有当前代码移动到另一个组件中,并使用上面显示的渲染函数创建一个新组件。然后nomsyapp 组件将具有navigator 属性,您将能够导航路线。

    【讨论】:

    • 所以我更改了我的 index.ios.js 文件以反映您所说的内容,他们使我当前的代码成为一个名为 Login.js 的视图 -> 但现在它只是最初路由到带有标题的空白页面在初始路线中通过..
    猜你喜欢
    • 2017-01-14
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多