undefined 不是 object(评估 '_this2.props.navigation.navigate')
undefined is not an object (evaluating '_this2.props.navigation.navigate')
单击 header 时,我希望它导航到下一页。但是,我不知道如何访问 class 之外的导航道具。关于如何做到这一点有什么建议吗?
import React,{Component} from 'react'
import { View, Text, TouchableOpacity, FlatList} from 'react-native'
class header extends Component {
render(){
return(
<TouchableOpacity
onPress={() => **this.props.navigation.navigate('PageTwo')**}
>
<Text>{'Go to next Page Two'}</Text>
</TouchableOpacity>
)
}
}
export default class PageOne extends Component {
static navigationOptions = {
title: 'Page One',
}
constructor(props) {
super(props);
this.state = {
data: // ...
};
}
_renderItem = ({item}) => (
// ...
);
render(){
return(
<FlatList
ListHeaderComponent={header}
data={this.state.data}
renderItem={this._renderItem}
/>
)
}
}
我假设是 React Navigation?
使用 withNavigation
HOC (https://github.com/react-community/react-navigation/blob/aead8ff9fbd2aceb2d06c8049c8ad0d55d77b5ab/docs/api/withNavigation.md)
对于 React 导航
首先确保您在项目中安装了 react-navigation lib。
如果不是,则在 cmd 中 运行 "npm install --save react-navigation"。
然后创建一个 App.js 文件来保存所有路由器名称,如
App.js :
import React from 'react';
import {
View,
Text,
StyleSheet,
} from 'react-native';
import {StackNavigator} from 'react-navigation';
import Splash from './src/components/Splash/Splash'
import Login from './src/components/Splash/Login'
const Navigation = StackNavigator({
Splash : {
screen : Splash
},
Login : {
screen : Login
},
})
export default Navigation;
在我的例子中,src 是用于存储所有 js 文件的脏文件。
现在在 index.android.js 中设置 AppRegistry.registerComponent。
index.android.js
import React, { Component } from 'react';
import {
AppRegistry,
Text,
View
} from 'react-native';
import {StackNavigator} from 'react-navigation';
import Splash from './src/components/Splash/Splash'
import Navigation from './App'
export class FirstReactDemo extends Component {
render() {
return (
<Splash/>
);
}
}
AppRegistry.registerComponent('FirstReactDemo', () => Navigation);
现在,在首屏设置onclick监听器。
Splash.js
import React, { Component } from 'react';
import {
View,
Text,
Button,
StyleSheet,
} from 'react-native';
import {StackNavigator} from 'react-navigation';
export default class SplashScreen extends Component {
static navigationOptions = {
title: 'Splash',
};
render() {
const { navigate } = this.props.navigation;
return (
<View>
<Text
onPress={() =>navigate('Login')}
style={styles.text}>My Splash screen</Text>
</View>
);
}
}
为下一个屏幕创建 Login.js 文件。
在使用您的子组件时自己传递 navigation = {navigation}
。
单击 header 时,我希望它导航到下一页。但是,我不知道如何访问 class 之外的导航道具。关于如何做到这一点有什么建议吗?
import React,{Component} from 'react'
import { View, Text, TouchableOpacity, FlatList} from 'react-native'
class header extends Component {
render(){
return(
<TouchableOpacity
onPress={() => **this.props.navigation.navigate('PageTwo')**}
>
<Text>{'Go to next Page Two'}</Text>
</TouchableOpacity>
)
}
}
export default class PageOne extends Component {
static navigationOptions = {
title: 'Page One',
}
constructor(props) {
super(props);
this.state = {
data: // ...
};
}
_renderItem = ({item}) => (
// ...
);
render(){
return(
<FlatList
ListHeaderComponent={header}
data={this.state.data}
renderItem={this._renderItem}
/>
)
}
}
我假设是 React Navigation?
使用 withNavigation
HOC (https://github.com/react-community/react-navigation/blob/aead8ff9fbd2aceb2d06c8049c8ad0d55d77b5ab/docs/api/withNavigation.md)
对于 React 导航 首先确保您在项目中安装了 react-navigation lib。 如果不是,则在 cmd 中 运行 "npm install --save react-navigation"。 然后创建一个 App.js 文件来保存所有路由器名称,如
App.js :
import React from 'react';
import {
View,
Text,
StyleSheet,
} from 'react-native';
import {StackNavigator} from 'react-navigation';
import Splash from './src/components/Splash/Splash'
import Login from './src/components/Splash/Login'
const Navigation = StackNavigator({
Splash : {
screen : Splash
},
Login : {
screen : Login
},
})
export default Navigation;
在我的例子中,src 是用于存储所有 js 文件的脏文件。
现在在 index.android.js 中设置 AppRegistry.registerComponent。
index.android.js
import React, { Component } from 'react';
import {
AppRegistry,
Text,
View
} from 'react-native';
import {StackNavigator} from 'react-navigation';
import Splash from './src/components/Splash/Splash'
import Navigation from './App'
export class FirstReactDemo extends Component {
render() {
return (
<Splash/>
);
}
}
AppRegistry.registerComponent('FirstReactDemo', () => Navigation);
现在,在首屏设置onclick监听器。
Splash.js
import React, { Component } from 'react';
import {
View,
Text,
Button,
StyleSheet,
} from 'react-native';
import {StackNavigator} from 'react-navigation';
export default class SplashScreen extends Component {
static navigationOptions = {
title: 'Splash',
};
render() {
const { navigate } = this.props.navigation;
return (
<View>
<Text
onPress={() =>navigate('Login')}
style={styles.text}>My Splash screen</Text>
</View>
);
}
}
为下一个屏幕创建 Login.js 文件。
在使用您的子组件时自己传递 navigation = {navigation}
。