React Native的导入导出
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的导入导出的更多相关文章
- 前端 vue/react 或者 js 导入/导出 xlsx/xls (带样式)表格的功能
第一种导出表格的功能: yarn add xlsx script-loader file-saver xlsx-style 效果展示 xlsx-style的bug修复:node_module/xlsx ...
- React Native知识12-与原生交互
一:原生传递参数给React Native 1:原生给React Native传参 原生给JS传数据,主要依靠属性. 通过initialProperties,这个RCTRootView的初始化函数的参 ...
- React Native 导入原生Xcode项目总结与记录
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
- react native 入门实践
上周末开始接触react native,版本为0.37,边学边看写了个demo,语法使用es6/7和jsx.准备分享一下这个过程.之前没有native开发和react的使用经验,不对之处烦请指出.希望 ...
- [转] 在React Native中使用ART
http://bbs.reactnative.cn/topic/306/%E5%9C%A8react-native%E4%B8%AD%E4%BD%BF%E7%94%A8art 前半个月捣腾了一下Rea ...
- React Native (一) 入门实践
上周末开始接触react native,版本为0.37,边学边看写了个demo,语法使用es6/7和jsx.准备分享一下这个过程.之前没有native开发和react的使用经验,不对之处烦请指出.笔者 ...
- 《React Native 精解与实战》书籍连载「iOS 平台与 React Native 混合开发」
此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...
- 《React Native 精解与实战》书籍连载「Node.js 简介与 React Native 开发环境配置」
此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...
- 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- ...
随机推荐
- Caffe模型读取
caffe模型最终保存使用过的protobuf形式,将一个已经训练好的caffe模型读取出来,可以参考如下: 1,包含的头文件: #include <google/protobuf/io/cod ...
- CodeIgniter框架——CI中视图路径问题
答: 视图中的所有路径全部和 index.php 同级,也就是和 index.php 属于一个目录下,也就是网站根目录. 因为 index.php 后面看似是路径的东西其实那只是一种 URL 参数而已 ...
- 【BZOJ2242】[SDOI2011]计算器 BSGS
[BZOJ2242][SDOI2011]计算器 Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ ...
- 《从零开始学Swift》学习笔记(Day 52)——Cocoa错误处理模式
原创文章,欢迎转载.转载请注明:关东升的博客 Swift错误处理模式,在Swift 1.x和Swift 2.0是不同的两种模式. Swift 1.x代码错误处理模式采用Cocoa框架错误处理模式,到现 ...
- VS2012如何显示行号
Tools-Options-Text Editor-All Languages –General – Display
- SSL逐渐演变到TLS
w https://zh.wikipedia.org/wiki/超文本传输安全协议
- vertical-align:middle;一般用于img和行内文字对齐方式
vertical-align:top ;文字和行内块元素的顶部对齐 vertical-align:middle;居中 vertical-align:bottom;底对齐
- 前端基础 & 初识JS(JavaScript)
JavaScript概述 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中),后将其改名ScriptEase(客 ...
- 0x08 MySQL 超详细-索引原理&慢查询优化【转-多图】(重点)
Content From——Egon's Blog http://www.cnblogs.com/linhaifeng/articles/7274563.html#top 0x01 介绍 为何要有索引 ...
- (4.16)sql server迁移DB文件(同一DB内)
SQL Server 修改数据库物理文件存在位置 关键词:迁移文件,迁移temp库(这怎么迁移呢,用方法2即可,需要重启实例) 三种均需要离线: 一共分为(1)脱机迁移和(2)在线迁移. (1)迁移方 ...