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,
},
});

React Native的导入导出的更多相关文章

  1. 前端 vue/react 或者 js 导入/导出 xlsx/xls (带样式)表格的功能

    第一种导出表格的功能: yarn add xlsx script-loader file-saver xlsx-style 效果展示 xlsx-style的bug修复:node_module/xlsx ...

  2. React Native知识12-与原生交互

    一:原生传递参数给React Native 1:原生给React Native传参 原生给JS传数据,主要依靠属性. 通过initialProperties,这个RCTRootView的初始化函数的参 ...

  3. React Native 导入原生Xcode项目总结与记录

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  4. react native 入门实践

    上周末开始接触react native,版本为0.37,边学边看写了个demo,语法使用es6/7和jsx.准备分享一下这个过程.之前没有native开发和react的使用经验,不对之处烦请指出.希望 ...

  5. [转] 在React Native中使用ART

    http://bbs.reactnative.cn/topic/306/%E5%9C%A8react-native%E4%B8%AD%E4%BD%BF%E7%94%A8art 前半个月捣腾了一下Rea ...

  6. React Native (一) 入门实践

    上周末开始接触react native,版本为0.37,边学边看写了个demo,语法使用es6/7和jsx.准备分享一下这个过程.之前没有native开发和react的使用经验,不对之处烦请指出.笔者 ...

  7. 《React Native 精解与实战》书籍连载「iOS 平台与 React Native 混合开发」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  8. 《React Native 精解与实战》书籍连载「Node.js 简介与 React Native 开发环境配置」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  9. React Native的语法之ES5和ES6

    原文地址:http://www.devio.org/2016/08/11/React-Native%E4%B9%8BReact%E9%80%9F%E5%AD%A6%E6%95%99%E7%A8%8B- ...

随机推荐

  1. ubuntu中怎样添加或删除一个PPA源

    添加PPA源的命令为:sudo add-apt-repository ppa:user/ppa-name 添加好更新一下: sudo apt-get update删除命令格式则为:sudo add-a ...

  2. asp.net中TextBox只能输入数字的最简洁的两种方法

    如下TextBox <asp:textboxonkeypress="isnum()"id="TextBox1"runat="server&quo ...

  3. Shader 优化笔记

    如果shader中采了深度图,但是实际上相机没开深度图的话,会严重降帧. 做uv动画时应该 o.uv1.xy = v.uv * _Layer1_ST.xy + frac(_Layer1_ST.zw * ...

  4. scrapy 简介

    Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架. Scrapy架构图(绿线是数据流向): Scrapy Engine(引擎): 负责Spider.ItemPip ...

  5. NewtonSoft.Json NULL转空字符串

    from:http://www.cnblogs.com/hetuan/articles/4565702.html NewtonSoft.Json对需要转为JSON字符串的对象的NULL值以及DBNul ...

  6. 【BZOJ4444】[Scoi2015]国旗计划 双指针+倍增

    [BZOJ4444][Scoi2015]国旗计划 Description A国正在开展一项伟大的计划——国旗计划.这项计划的内容是边防战士手举国旗环绕边境线奔袭一圈.这项计划需要多名边防战士以接力的形 ...

  7. SharePoint server 2016中文版导出list template,在另外一个环境不能显示

    SharePoint server 2016中文版导出list template,在另外一个环境不能显示,解决方案: $web = Get-SPWeb <url of web> $web. ...

  8. A Secret(KMP)

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total ...

  9. Introspection in Python How to spy on your Python objects Guide to Python introspection

    Guide to Python introspection https://www.ibm.com/developerworks/library/l-pyint/ Guide to Python in ...

  10. 通过margin负值去除padding

    .pay-type { // 图片布局前通过margin负值去除padding margin: 0 -@page-padding-horizontal; display: inline-flex; } ...