我是新来的本地人。我使用了功能组件而不是工作正常的类。在这里,我不能使用构造函数,状态,生命周期方法等。所以我想创建一个类而不是函数但没有运气。
功能组件(index.android.js)
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
Button,
View
} from 'react-native';
import {
TabNavigator
} from 'react-navigation';
import MyHomeScreen from './MyHomeScreen';
import MyNotificationsScreen from './MyNotificationsScreen';
const TabNavigation = TabNavigator({
Home: {
screen: MyHomeScreen,
},
Notifications: {
screen: MyNotificationsScreen,
},
}, {
tabBarOptions: {
activeTintColor: '#e91e63',
},
});
AppRegistry.registerComponent('TabNavigation', () => TabNavigation);
MyHomeScreen.js
export default class MyHomeScreen extends React.Component {
static navigationOptions = {
tabBarLabel: 'Home',
tabBarIcon: ({ tintColor }) => (
<Image
source={require('./chats-icon.png')}
/>
),
};
render() {
return (
<Button
onPress={() => this.props.navigation.navigate('Notifications')}
title="Go to notifications"
/>
);
}
}
MyNotificationsScreen.js
export default class MyNotificationsScreen extends React.Component {
static navigationOptions = {
tabBarLabel: 'Notifications',
tabBarIcon: ({ tintColor }) => (
<Image
source={require('./notif-icon.png')}
/>
),
};
render() {
return (
<Button
onPress={() => this.props.navigation.goBack()}
title="Go back home"
/>
);
}
}
使用class:我想使用如下类。如何使其像上面名为TabNavigation的功能组件一样工作?
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
Button,
View
} from 'react-native';
import {
TabNavigator
} from 'react-navigation';
import MyHomeScreen from './MyHomeScreen';
import MyNotificationsScreen from './MyNotificationsScreen';
export default class TabNavigation extends Component {
render() {
return (
<View>
//what to do here
</View>
);
}
}
答案 0 :(得分:0)
如果要在顶层渲染TabNavigation,那么执行此操作的方法与第一个示例相同。 /kibana
构建一个组件。
您的用例在您的问题中似乎并不清楚,但如果您想要一个顶级的课程,您可以将TabNavigation包装在一个类中:
TabNavigator()