Mac 安裝react-native 環境踩坑記

 

個人工程建立時間是2019.7.11號下午   :首先看一下最後個人工程的package.json各個包的版本:html

{
"name": "MyApp",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.8.3",
"react-native": "0.59.8",
"react-native-gesture-handler": "^1.3.0",
"react-navigation": "^3.11.0"
},
"devDependencies": {
"@babel/core": "^7.5.4",
"@babel/runtime": "^7.5.4",
"babel-jest": "^24.8.0",
"jest": "^24.8.0",
"metro-react-native-babel-preset": "^0.55.0",
"react-test-renderer": "16.8.3"
},
"jest": {
"preset": "react-native"
}
}

 

1.安裝依賴node

brew install node    brew install watchman    注意 node>10  
react

安裝yarn ios

npm install -g yarn react-native-cli
git

 

安裝完 yarn 後同理也要設置鏡像源:github

yarn config set registry https://registry.npm.taobao.org --global yarn config set disturl https://npm.taobao.org/dist --global 
 
在Appstore下載Xcode  版本>10
React Native 目前須要Xcode 9.4 或更高版本。你能夠經過 App Store 或是到Apple 開發者官網上下載。
這一步驟會同時安裝 Xcode IDE、Xcode 的命令行工具和 iOS 模擬器。
建立新項目
安裝第三方依賴,再http://bbs.reactnative.cn/topic/4301/ios-rn-0-45以上版本所需的第三方編譯庫-boost等
選擇最高版本,複製到.rncahce文件夾下
須要安裝到根目錄下的.rncache文件夾 因此執行一下操做,

hewenkedeMacBook-Pro:AwesomeProject hewenke$ cd ~npm

hewenkedeMacBook-Pro:~ hewenke$ mkdir .rncachejson

hewenkedeMacBook-Pro:~ hewenke$ cp ~/Downloads/boost_1_63_0.tar.gz ~/.rncache/ react-native

hewenkedeMacBook-Pro:~ hewenke$ cp ~/Downloads/double-conversion-1.1.5.tar.gz ~/.rncache/ xcode

hewenkedeMacBook-Pro:~ hewenke$ cp ~/Downloads/double-conversion-1.1.6.tar.gz ~/.rncache/ 

hewenkedeMacBook-Pro:~ hewenke$ cp ~/Downloads/folly-2018.10.22.00.tar.gz ~/.rncache/ 

hewenkedeMacBook-Pro:~ hewenke$ cp ~/Downloads/glog-0.3.5.tar.gz ~/.rncache/ 

hewenkedeMacBook-Pro:~ hewenke$ cd .rncache

hewenkedeMacBook-Pro:.rncache hewenke$ ls

boost_1_63_0.tar.gz folly-2018.10.22.00.tar.gz

double-conversion-1.1.5.tar.gz glog-0.3.5.tar.gz

double-conversion-1.1.6.tar.gz

hewenkedeMacBook-Pro:.rncache hewenke$ rm double-conversion-1.1.5.tar.gz

hewenkedeMacBook-Pro:.rncache hewenke$ ls

boost_1_63_0.tar.gz folly-2018.10.22.00.tar.gz

double-conversion-1.1.6.tar.gz glog-0.3.5.tar.gz

安裝完第三方依賴,就能夠建立新項目
react-native init AwesomeProject
初始化結束之後cd AwesomeProject && react-native run-ios
就能夠啓動項目
===>注意事項 不要初始化0.44版本的項目。問題太多。
 
 
app.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/

import React, {Fragment} from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';

import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';

const App = () => {
return (
<Fragment>
<StatusBar barStyle="dark-content" style={{backgroundColor:'#99cc61'}}/>
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}>
<Header />
<View style={styles.body}>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Step One</Text>
<Text style={styles.sectionDescription}>
Edit <Text style={styles.highlight}>App.js</Text> to change this
screen and then come back to see your edits.
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>See Your Changes</Text>
<Text style={styles.sectionDescription}>
<ReloadInstructions />
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Debug</Text>
<Text style={styles.sectionDescription}>
<DebugInstructions />
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Learn More</Text>
<Text style={styles.sectionDescription}>
Read the docs to discover what to do next:
</Text>
</View>
<LearnMoreLinks />
</View>
</ScrollView>
</SafeAreaView>
</Fragment>
);
};

const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
body: {
backgroundColor: Colors.white,
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
color: Colors.black,
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
color: Colors.dark,
},
highlight: {
fontWeight: '700',
},
});

export default App;

頁面就能夠正常出來。

按照官網   須要路由,可是後期若是使用路由組件react-navigation 和react-native-gesture-handler 安裝這兩個庫之後,react-native>=0.6就會出錯

這兩個的安裝按照  官網來 鏈接  https://reactnavigation.org/docs/zh-Hans/getting-started.html#%E5%AE%89%E8%A3%85

這個時候須要吧版本降到0.6一下 好比0.59.8,我安裝的是0.59.8  react-native  init demo --version 0.59.8 從新新建一個工程, 完了執行react-native run-ios  就能夠運行了,這個編譯時間比教長,就能夠正常使用路由了
相關文章
相關標籤/搜索