React-native完整配置流程
tsconfig.json文件: { "compilerOptions": { "module":"es2015", "target": "es2015", "jsx": "react", //jsx要配置成react,默认情况下没有,不然jsx解析会失败 "rootDir": "src", //入口文件夹,默认情况下没有src文件夹,所以还要在项目下创建一个src文件夹进行入口的编译 "outDir": "build", //输出文件夹,ts必须打成一个包,把ts转成js无法运行文件,所以先build再去run,同时加上watch每修改一次build一次 "allowSyntheticDefaultImports": true, "noImplicitAny": true, "sourceMap": true, "experimentalDecorators": true, "preserveConstEnums": true, "allowJs": true, "noUnusedLocals": true, "noUnusedParameters": true, "noImplicitReturns": true, "skipLibCheck": true, "moduleResolution":"Node" }, "filesGlob": [ "typings/index.d.ts", "src/**/*.ts", "src/**/*.tsx", "node_modules/typescript/lib/lib.es6.d.ts" ], "types": [ "react", "react-dom", "react-native" ], "exclude":[ // exclude排除哪些配置项 "build", "node_modules", "jest.config.js", "App.js" ], "compileOnSave": false }
package.json文件:
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "node node_modules/jest/bin/jest.js",
"lint": "tslint src/**/*.ts",
"tsc": "tsc",
"clean": "rimraf build",
"build": "yarn run clean && yarn run tsc --",
"watch": "yarn run build -- -w",
"watchAndRunAndroid": "concurrently \"yarn run watch\" \"yarn run android\"",
"buildRunAndroid": "yarn run build && yarn run watchAndRunAndroid ",
"watchAndRunIOS": "concurrently \"yarn run watch\" \"yarn run ios\"",
"buildRunIOS": "yarn run build && yarn run watchAndRunIOS ",
"watchAndStart": "concurrently \"yarn run watch\" \"yarn run start\"",
"buildAndStart": "yarn run build && yarn run watchAndStart "
}
修改package.json中的入口文件:
"main":"./node_modules/react-native-scripts/build/bin/crna-entry.js"
此时可以看见node_modules/react-native-scripts/build/bin/crna-entry.js文件中的 var _App = require('../../../../App');
删除App.js的文件内容,粘贴以下内容:
App.js文件:
import App from './build/App';
export default App;
创建一个src文件夹目录,再将babel.config.js文件拖到src文件目录下!
配置完成,可以直接run了 。
yarn buildAndStart
App.tsx文件:
import React from "react"
import {
View,
Text
} from "react-native"
export default class componentName extends React.Component{
render(){
return(
<View>
<Text>hello world</Text>
</View>
)
}
}
这个时候可以联想到nextTick,我们应该等数据来了再渲染,你可以试着判断一下你的渲染数据: this.list.length>0? 渲染:"null"
import { Provider} from "mobx-react"
import { createStackNavigator } from "react-navigation
七、WebView
import { WebView } from "react-native"
import { Text, View, TouchableOpacity } from 'react-native' import { Camera, Permissions } from 'expo'
import { Switch } from "react-native"
import { AsyncStorage } from "react-native"
await AsyncStorage.setItem('isShowMap', `${value}`); // 第二个参数是字符串
const isShowMap = Boolean(await AsyncStorage.getItem('isShowMap')); // 返回值是一个字符串,需要转化为Boolean类型
十一、上拉刷新、下拉加载 — FlatList引入
import { FlatList } from "react-native"
上面只规整了import引入的方式,具体代码格式大家可以进RN官网再去看一下,当然!文章最上头有规整好网址,大家copy代码就能很happy的run了。
恩。RN就整理到这里,希望能帮助到大家。