React Native创建Component组件的方式

1 ES6创建组件

ES6创建Class基础Component:

import React, {Component} from 'react';
export default class MainView extends Component<Props> {
    render() {
        return (
            <View>
                <Text>这个是主界面!</Text>
            </View>
        );
    }
}

2 ES5创建组件

var MainView =React.createClass( {
    render() {
        return (
            <View>
                <Text>这个是主界面!</Text>
            </View>
        );
    }
});

3  函数式

无状态的形式,不能使用this;

1.MainComponent .js

function MainComponent(pros){
  return(<Text>姓名:{pros.name}</Text>);
}

module.exports = MainComponent;

2. 其他js文件调用函数:

import MainComponent from '/MainComponent '


export default class simple extends Component {
render() { 
 return (
 <View style={styles.container}> 
     <HelloComponent name = "小明"/> //使用HelloComponent组件 
 </View> );
} }

猜你喜欢

转载自blog.csdn.net/niuba123456/article/details/82119222