react-native 之导入、导出及使用
在学习使用React-Native过程中,对于组件、类、方法和变量的导入、导出并涉及到使用,这一整套过程,我们要熟知。此乃React-Native开发之必备基础。接下来,我就用一个小demo来详尽的阐述一下他们的使用方式; /** * Created by YJH on 2018/5/24. */
import React,{Component} from 'react';
import {
StyleSheet,Text,Image,View,} from 'react-native';
/** * 功能:定义一个组件、变量,通过该组件、变量来展示其导出和导入以及使用 */
export var FLAG_NAME={flag_game_gun:'全民出击',flag_game_king:'王者荣耀'};
export default class ExportComponent extends Component<Props> {
constructor(props) {
super(props);
}
render() {
return (
<View style={styles.container}>
<Text style={styles.export_des}>ExportComponent.js</Text>
<Image style={{width:100,height:100}} source={require('../images/timg.jpg')}/>
</View>
)
}
}
const styles = StyleSheet.create(
{
container: {
flex: 1,alignItems:'center'
},export_des: {
marginTop: 20,fontSize: 30,color: 'orange',alignSelf: 'center',}
}
);
类的代码是这样的 /** * 功能:定义一个类,通过该类来展示其导出和导入以及使用 */
export default class ExportClass{
isEqualed(newWord,oldWord){
if(!newWord || !oldWord)return false;
if(newWord !== oldWord)return false;
return true;
}
}
方法的代码是这样的 /** * 功能:定义一个方法,通过该方法来展示其导出和导入以及使用 * @param name * @param color */
export default function ExportFunction(name,color) {
this.name=name;
this.color=color;
}
导入使用的方式是这样的 /** * Created by YJH on 2018/5/24. */
import React,} from 'react-native';
import ExportComponent,{FLAG_NAME} from './js/component/ExportComponent';
import ExportFunction from './js/function/ExportFunction';
import ExportClass from './js/class/ExportClass';
export default class WelcomePage extends Component<Props> {
constructor(props) {
super(props);
this.exportClass=new ExportClass();
this.exportFunction = new ExportFunction(FLAG_NAME.flag_game_gun,'green');
this.state = {
disPlayNme: this.exportFunction.name,disPlayColor: this.exportFunction.color,isEqualed:false,}
}
//当UI已render调用
componentDidMount(){
//通过类 ExportClass 的方法比较来动态改变isEqualed的属性值
const equaled=this.exportClass.isEqualed('hello world','hello react-native');
this.setState({
isEqualed:equaled,})
}
render() {
return (
<View style={styles.container}>
<Text style={{
marginTop: 20,color: this.state.disPlayColor,}}>欢迎来到{this.state.disPlayNme}</Text>
<ExportComponent {...this.props}/>
</View>
)
}
}
const styles = StyleSheet.create(
{
container: {
flex: 1,},}
);
OK!!通过上面的demo代码,对组件、类、方法、变量的导入、导出和使用进行分解详述!! 导出组件变量 导出类 导出方法 导入组件、类、方法和变量 当然,我们之所以要导入,目的必定是要使用的。一起来看下使用方式 类和方法的初步使用 类和方法的具体使用 再看方法的使用 使用方式上就这么多,完结!demo地址 此处增加一个
|