react native 项目初始化

初始化项目

1
2
3
4
5
npx react-native init projectname --template react-native-template-typescript

# 启动项目
yarn ios
yarn android

多环境

https://js.coach/package/react-native-config

1
yarn add react-native-config

根目录下新建 .env

1
2
API_URL=https://myapi.com
GOOGLE_MAPS_API_KEY=abcdefgh

使用

1
2
3
4
import Config from "react-native-config";

Config.API_URL; // 'https://myapi.com'
Config.GOOGLE_MAPS_API_KEY; // 'abcdefgh'

安装

ios

1
(cd ios; pod install)

android
android/app/build.gradle

1
2
// 2nd line, add a new apply:
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

绝对路径

https://www.npmjs.com/package/babel-plugin-module-resolver

1
yarn add babel-plugin-module-resolver

配置 .babelrcbabel.config.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
[
'module-resolver',
{
root: ['./src'],
alias: {
'@': './src',
},
},
],
],
};

配置 tsconfig.json

1
2
3
4
5
6
{
"baseUrl": "./src",
"paths": {
"@/*": ["*"],
},
}