加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

React Native es6继承(Component例子)

发布时间:2020-12-15 03:30:24 所属栏目:百科 来源:网络整理
导读:很多用React Native的同学都是前端工程师,在传统的Js没有继承的概念。但是在react Native所支持的es6是有继承的,今天牛刀小试一下,原来效果也是不错的,分享给大家。 首先定义一个BaseComponent,例如有一个fullName的方法 import React,{ Component } fr

很多用React Native的同学都是前端工程师,在传统的Js没有继承的概念。但是在react Native所支持的es6是有继承的,今天牛刀小试一下,原来效果也是不错的,分享给大家。

首先定义一个BaseComponent,例如有一个fullName的方法

import React,{ Component } from 'react';


export default class BaseComponent extends Component {  
  constructor(props) {
    super(props);
  }

  fullName() {
    return 'test'
  }
}

定义一个类,运行的时候,动态读取父类的方法
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,},});

最终读取父类的方法

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读