Undefined不是一个对象(评估'this.props.navigator.push')在React Native中

时间:2017-03-23 07:09:23

标签: javascript reactjs mobile react-native

  

这里我需要将一个变量传递给HomeContainer.But当我点击GO TO ABOUT按钮时我得到了错误。我是React Native的新手。所以我不确定我是否按照正确的方法来将数据传递给另一个视图。希望有人能帮助我。谢谢。

import React, { Component } from 'react'
import {
   AppRegistry,
   Navigator,
   View
} from 'react-native'
import Router from './src/components/Router'

class navigation extends Component {
    render() {
    return (
      <Navigator 
      initialRoute={{name: 'Router', component: Router, index: 0}}
      renderScene={(route, navigator) => {
        return React.createElement(route.component, { ...this.props, ...route.passProps, navigator, route } );
      }} />
    );
  }
}
AppRegistry.registerComponent('navigation', () => navigation)



    import React, { Component } from 'react'

import {
    AppRegistry,
  StyleSheet,
  Text,
  View,
  Navigator,
  TouchableHighlight,
  TextInput
} from 'react-native'

import AboutContainer from './about/AboutContainer'
import HomeContainer from './home/HomeContainer'

export default class Router extends Component {
   constructor(){
      super()
   }
 getInitialState() {
    return {}
  }

  _goToHome() {
    this.props.navigator.push({
        component: HomeContainer,
        passProps: {
          username: this.state.username,
        }
    })
  }

  updateUsername(username) {
    this.setState({ username })
  }

    render() {
      return (

        <View style={styles.container}>
          <TouchableHighlight 
            onPress={ () => this._goToHome() }
            style={ styles.button }>
            <Text>
                GO TO ABOUT
            </Text>
          </TouchableHighlight>
          <TextInput 
            onChangeText={ (username) => this.updateUsername(username) }
            placeholder="Your Username"
            style={ styles.input }
          />
       </View>
     )
   }
}

0 个答案:

没有答案