Mac 安装react-native 环境踩坑记
我的工程创建时间是2019.7.11号下午 :首先看一下最后我的工程的package.json各个包的版本:
{
"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.安装依赖
brew install node brew install watchman 注意 node>10
安装yarn
npm install -g yarn react-native-cli
安装完 yarn 后同理也要设置镜像源:
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 模拟器。
创建新项目
hewenkedeMacBook-Pro:AwesomeProject hewenke$ cd ~
hewenkedeMacBook-Pro:~ hewenke$ mkdir .rncache
hewenkedeMacBook-Pro:~ hewenke$ cp ~/Downloads/boost_1_63_0.tar.gz ~/.rncache/
hewenkedeMacBook-Pro:~ hewenke$ cp ~/Downloads/double-conversion-1.1.5.tar.gz ~/.rncache/
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
/**
* 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