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. 黑马day11 事务的四大特性

    1.事务的四大特性:一个事务具有的最主要的特性.一个设计良好的数据库能够为我们保证这四大特性. 1.1原子性:原子性是指事务是一个不可切割的工作单位,事务中的操作要么都发生要么都不发生. 1.2一致性 ...

  2. MathType编辑表示右上角的符号的方法

    MathType是一款公式编辑器,其中的符号种类非常的全,如果需要编辑一些专业的公式,用它非常的方便快捷.但是有些符号由于不常用,在一些模板之中使用的时候需要深入地去查找调用一下.比如表示右上角的符号 ...

  3. caffe net 可视化工具,,层特征可视化

    1.只用网络在线结构绘制可视化网络模型 http://ethereon.github.io/netscope/#/editor 将对应的网络输入到里面,然后按shift+enter即可查看对应的网络结 ...

  4. C++ 虚析构(virtual destructor)原理

    注意:本文仅为个人理解,可能有误! 先看一段代码: #include <iostream> using namespace std; class CBase{ public: CBase( ...

  5. EditText相关属性设置

    1.默认不弹出软件盘 在AndroidManifest.xml设置: <activity            android:name="com.demo.Activity" ...

  6. hibernate 标签inverse cascade

    inverse设立不当会导致性能低下,其实是说inverse设立不当,会产生多余重复的SQL语句甚至致使JDBC exception的throw.这是我们在建立实体类关系时必须需要关注的地方.一般来说 ...

  7. openssl 升级操作 -2

    首先我觉得没事就用绿盟扫漏洞的公司,就是闲的蛋疼,傻逼!不少服务器使用nginx,如果openssl 是静态编译的,直接将openssl 编译到nginx里面去了,这就意味着,单纯升级openssl ...

  8. Cocos2d-x Lua Node与Node层级架构

    Cocos2d-x Lua采用层级(树形)结构管理场景.层.精灵.菜单.文本.地图和粒子系统等节点(Node)对象.一个场景包含了多个层,一个层又包含多个精灵.菜单.文本.地图和粒子系统等对象.层级结 ...

  9. jQuery之获取select选中的值

    本来以为jQuery("#select1").val();是取得选中的值, 那么jQuery("#select1").text();就是取得的文本. 这是不正确 ...

  10. iOS蓝牙接收外设数据自动中断

    一.错误原因 在做iOS设备作为central,与蓝牙外设连接,接收蓝牙外设传输的数据时发生蓝牙中断. 在- (void)centralManager:(CBCentralManager *)cent ...