1、组件的导入导出方式

问1:如何导出一个组件?

export default class EIComponent extends Component{
render(){
return(
<Text style = {{fontSize:20,backgroundColor:'red'}}>hello.</Text>
);
}
}

问2:如何在其他组件中使用导出的组件?

import HelloComponent from './HelloComponent';

2、导出一个或多个变量

问1:如何导出一个或多个变量?

export var name = '小明';
export var age = '22';
// export {name,age};

问2:如何使用导出变量?

import HelloComponent,{name,age} from './HelloComponent';

3、导入导出方法

问1:如何导出方法?

export function sum(a,b){
return a + b;
}

问2:如何使用导出的方法?

import HelloComponent,{name,age,sum} from './HelloComponent';

实例代码:

  导出组件、变量、方法

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
export var name = '小明';
export var age = '22';
// export {name,age};
export default class EIComponent extends Component{
render(){
return(
<Text style = {{fontSize:20,backgroundColor:'red'}}>hello.</Text>
);
}
} export function sum(a,b){
return a + b;
}

  使用导出的组件、变量、方法

/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
* @lint-ignore-every XPLATJSCOPYRIGHT1
*/ import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import HelloComponent,{name,age,sum} from './HelloComponent';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
}); type Props = {};
export default class App extends Component<Props> {
constructor(props){
super(props);
this.state =({
result:''
})
}
render() {
return (
<View style={styles.container}>
<Text style ={styles.welcome}>名字:{name}</Text>
<Text style ={styles.welcome}>年龄:{age}</Text>
<Text style ={styles.welcome}
onPress={()=>{
var result = sum(2,3);
this.setState({
result:result
})
}}
>2+3={this.state.result}</Text>
</View> );
}
} const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'white',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

最新文章

  1. El表达式的关系运算符
  2. url中的特殊字符问题
  3. T-Sql(六)触发器(trigger)
  4. 图解GCD
  5. 使用替换shader渲染
  6. Maven异常Type Project configuration is not up-to-date with pom.xml. Run Maven-&gt;Update Project or use Quick Fix
  7. kali linux系列之启用vpn
  8. 对象生命周期及crud操作
  9. WPF 绑定三(绑定List中指定的字符串)
  10. java_jdbc_反射
  11. Python Set集合,函数,深入拷贝,浅入拷贝,文件处理
  12. Objective-c 算术函数和常量代表
  13. 微信企业号 JS-SDK:上传图片
  14. php笔记(三)PHP类和对象之访问控制
  15. 从源码角度看LinkedList一些基本操作(jdk1.7)
  16. Git实操
  17. 【Linux】时间同步设置+防火墙设置+SELinux设置
  18. 课程四(Convolutional Neural Networks),第三 周(Object detection) —— 2.Programming assignments:Car detection with YOLOv2
  19. 树莓派2B+安装Debain操作系统
  20. jquery基础学习之样式篇(一)

热门文章

  1. java字符编码详解
  2. oracle密码过期解决方法
  3. EntityFramework(EF) 单表与主从表的使用
  4. GoogleMap-------manifest文件配置
  5. OBS (open boardcast server)结构分析
  6. python3 - 动态添加属性以及方法
  7. std::condition_variable(2)复习
  8. Win7系统安装 MySQL 8.0.11
  9. ul和li弄的图片列表
  10. iOS 多线程之 GCD 的基本使用