一般新创建的RN项目基本就是以index文件作为入口文件,在此文件里可以更换组件~
第一步:在项目的根目录下新建一个自己的文件夹,我创建的是my_pages ,我们来创建一个自己的测试样式的MyStyle.js文件,编辑自己的文件内容...
import React,{Component} from 'react'; import {StyleSheet, Text, View} from 'react-native' class MyStyle extends Component{ render() { return( <View> <Text style={styles.red}>hello red</Text> <Text style={styles.bigblue}>hello bigblue</Text> </View> ); } } //创建自己的样式组 const styles=StyleSheet.create({ bigblue:{ color:'blue', fontWeight:'bold', fontSize:30 }, red:{ color:'red' }, });
// 默认导出MyStyle组件(这是指定组件的关键) export default MyStyle
第二步:找到index.js文件,修改如下(注意指定文件夹下面的文件),可以左键点击类名,跳转查看
import { AppRegistry } from 'react-native'; import MyStyle from './my_pages/MyStyle'; AppRegistry.registerComponent('Hello', () => MyStyle);
效果图:
好啦,完成了。