【问题标题】:Undefined is not an object( evaluating this.props.navigator.push')未定义不是对象(评估 this.props.navigator.push')
【发布时间】:2017-02-17 08:17:51
【问题描述】:

我正在学习react-native android 应用程序编程。我试图在按下TouchableOpacity 时启动第二个屏幕。我正在使用navigator

我收到此错误undefined is not an object( evaluating this.props.navigator.push')

我检查了很多线程 React Native, NavigatorIOS, undefined is not an object (evaluating 'this.props.navigator.push') undefined is not an object(evaluating this.props.navigator.push) 但对我不起作用。我不确定我在这里做错了什么,谁能帮助我。提前致谢。

index.android.js

/**
* https://github.com/facebook/react-native
* @flow
*/

import React, { Component } from 'react';
import { AppRegistry, Text, View, TextInput, TouchableOpacity, ToolbarAndroid, StyleSheet,Container,ScrollView, Navigator } from 'react-native';


class App extends Component {
  renderScene (route, navigator) {
    return <route.component navigator={navigator} />
  }
  render() {
    return (
        <Navigator
          style={styles.container}
          renderScene={this.renderScene.bind(this)}
          initialRoute={{component: LoginComponent}}
        />
    );
  }
}

class LoginComponent extends Component {

  _navigate () {
      this.props.navigator.push({
          component: DashboardComponent
      })
  }

  render() {
     return (
          <View style={{flex: 1, flexDirection: 'column'}}>

            <ToolbarAndroid title='LOGIN' titleColor='white'
                onIconClicked={() => this.props.navigator.pop()}
                style={styles.toolbar}/>

            <View style={{padding:10}}>

              <TextInput style={{height: 40, borderColor:'gray', borderWidth: .5}}
                  placeholder="Email address" underlineColorAndroid='transparent'/>

              <TextInput style={{height: 40, borderColor:'gray', borderWidth: .5}}
                  placeholder="Password" secureTextEntry={true} underlineColorAndroid='transparent'/>

              <TouchableOpacity style={{ height: 40, marginTop: 10 , backgroundColor: '#2E8B57'}} onPress={this._navigate.bind(this)}>
                  <Text style={{color: 'white', textAlign: 'center', marginTop: 10, fontWeight: 'bold'}}>LOGIN</Text>
              </TouchableOpacity>
            </View>
       </View>
     );
  }
}

const styles = StyleSheet.create({
    toolbar: {
     backgroundColor: '#2E8B57',
     height: 40,
     fontFamily: 'noto_serif_regular',
    },
});

AppRegistry.registerComponent('ExampleOne', () => LoginComponent);

second.android.js

import React, { Component } from 'react';

class DashboardComponent extends Component {
  render() {
     return (
       <Text>Hello!</Text>
     );
   }
 }

【问题讨论】:

  • 您的代码看起来不错,在您的TouchableOpacity 上试试这个onPress={() =&gt; this._navigate()}
  • 我正在尝试调用另一个文件中的第二个组件。当我试图打电话时,我收到一个错误can't find variable: DashboardComponent

标签: android react-native react-native-android


【解决方案1】:

你在AppRegistry.registerComponent注册了错误的组件,它应该是App而不是LoginComponent

导航器组件需要先渲染,然后它会渲染并将导航器道具传递给它的场景。

【讨论】:

  • +1 我正在尝试调用另一个文件中的第二个组件。当我尝试调用时,我收到一个错误找不到变量:DashboardComponent
  • 你需要导入DashboardComponent,即import DashboardComponent from './DashboardComponent,假设DashboardComponent.js和你的index.android.js是同一个文件夹
  • 现在我收到一个错误未捕获错误:不变违规:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数,但得到:对象。检查Navigator的渲染方法。
  • 我想你忘了导出你的DashboardComponent,只需在second.android.js 中添加export default DashboardComponent;,然后在index.android.js 中将其导入为import DashboardComponent from './second';
  • 过渡时有一些滞后。这可能是什么原因
猜你喜欢
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
  • 2016-12-04
  • 2019-09-26
  • 2019-12-12
  • 2019-08-16
  • 2017-01-16
相关资源
最近更新 更多