React-native完整配置流程

開頭敲黑板!!
  不管你是RN的新手仍是老手,跟着流程走,RN項目搭建起來徹底不是問題!
 
1、網址收集
tabbar樣式設置:https://www.npmjs.com/package/react-native-navigator
switch網址: https://reactnavigation.cn/docs/switch/
 
2、安裝
yarn global add create-react-native-app
create-react-native-app cookbooks
        ? Choose a template: blank
yarn add typescript tslint --dev     能夠把ts的錯誤暴露出來
yarn add @types/react @types/react-native @types/react-dom --dev  react-dom安裝後能夠基於瀏覽器使用ts
yarn add concurrently rimraf  react-native-scripts --dev   能夠切換環境()端口號的自動分配
yarn add ts-jest @types/jest @types/react-test-renderer --dev
tsc --init    初始化,能夠不用該命令,手工建立tsconfig.json文件
                 若使用該命令會自動生成一個tsconfig.js文件,刪除裏面全部內容,將下面的配置信息徹底拷貝過去 
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文件中的npm expo-cli腳本scripts
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的文件內容,粘貼如下內容:javascript

App.js文件:
import App from './build/App';
export default App;

建立一個src文件夾目錄,再將babel.config.js文件拖到src文件目錄下!html

 

配置完成,能夠直接run了 。前端

yarn buildAndStartjava

此時會自動生成一個build文件,但裏面只有babel.config.js文件,但咱們須要讓build裏有App.js文件
因此須要在src下建立一個App.tsx文件,這時候你能夠想寫什麼就寫什麼了,固然你也能夠用下面的代碼試試效果。
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>
        )
    }
} 
 
3、tabbar 引入
yarn add react-native-tab-navigator 這個這個可能用到,可能用不到,若是找不到navigation,能夠兩個都裝一下,很是靠譜!
yarn add @types/react-native-tab-navigator
 
4、Swiper引入
yarn add react-native-swiper
注意:引入Swipper的時候,你可能會發現swiper小點點不動了?
這個時候能夠聯想到nextTick,咱們應該等數據來了再渲染,你能夠試着判斷一下你的渲染數據:
this.list.length>0? 渲染:"null"
 
5、MobX引入
yarn add mobx
yarn add mobx-react    須要在App.tsx中引入provider、store
import { Provider} from "mobx-react"
 
6、路由引入
yarn add  react-navigation@2.18.3     有版本限制,須要注意
yarn add  @types/react-navigation@2.0.24  
import { createStackNavigator } from "react-navigation

 

7、WebViewnode

import { WebView } from "react-native"
採用原生的方法把H5頁面嵌入進去
注意:react-native是用前端技術開發原生APP,它不是混合式開發
 
8、camera引入(引入第三方包都須要提供ts支持)
yarn add @types/expo
import { Text, View, TouchableOpacity } from 'react-native'
import { Camera, Permissions } from 'expo'
Flip:鏡頭反轉
點擊拍照能夠拿到圖片的url,其實圖片已經存入本地
react-native能夠將全部的硬件設備掛起
 
9、switch引入
import { Switch } from "react-native" 
 
10、AsyncStorage引入
  異步、持久的Key-Value存儲系統
import { AsyncStorage } from "react-native"
await AsyncStorage.setItem('isShowMap', `${value}`);    // 第二個參數是字符串
const isShowMap =  Boolean(await AsyncStorage.getItem('isShowMap'));   // 返回值是一個字符串,須要轉化爲Boolean類型

 

11、上拉刷新、下拉加載 -- FlatList引入  react

import { FlatList } from "react-native"

 

上面只規整了import引入的方式,具體代碼格式你們能夠進RN官網再去看一下,固然!文章最上頭有規整好網址,你們copy代碼就能很happy的run了。android

恩。RN就整理到這裏,但願能幫助到你們。ios

相關文章
相關標籤/搜索