很多用React Native的同學(xué)都是前端工程師,在傳統(tǒng)的js沒(méi)有繼承的概念。但是在react Native所支持的es6是有繼承的,效果也是不錯(cuò)的,分享給大家。
首先定義一個(gè)BaseComponent,例如有一個(gè)fullName的方法
import React, { Component } from 'react';
export default class BaseComponent extends Component {
constructor(props) {
super(props);
}
fullName() {
return 'test'
}
}
定義一個(gè)類,運(yùn)行的時(shí)候,動(dòng)態(tài)讀取父類的方法
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
import BaseComponent from './BaseComponent';
export default class PageComponent extends BaseComponent {
render() {
return (
<View style={{flex: 1,paddingTop: 50,}}>
<Text style={styles.welcome}>
{ this.fullName() }
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});
最終讀取父類的方法