0
我是新的反應原生。我已經使用了功能組件,而不是使用正常工作的類。在這裏,我不能使用構造函數,狀態,生命週期方法等。所以我想創建一個類而不是函數,但沒有運氣。使用類而不是無狀態的功能組件 - 反應本機
功能部件(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"
/>
);
}
}
使用類:我想用類,如下所示。我如何使它在像上面那樣名爲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>
);
}
}