怎么在ReactNative里面使用Typescript

今天来搞一搞怎么搭建一个可以使用Typescript的ReactNative环境好吧,一句废话不多说,直接开始好吧

1.全局安装create-react-native-app

yarn global add create-react-native-app

   

<div class="cnblogs_code">

例如:create-react-native-app  myApp

  

5.继续安装其他依赖

6.通过tsc --init生成tsconfig.json,或者手动创建一个tsconfig.json,将下面代码拷入该文件

  

"compilerOptions""module":"es2015""target": "es2015""jsx": "react""rootDir": "src""outDir": "build""allowSyntheticDefaultImports": "noImplicitAny": "sourceMap": "experimentalDecorators": "preserveConstEnums": "allowJs": "noUnusedLocals": "noUnusedParameters": "noImplicitReturns": "skipLibCheck": "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""build""node_modules""jest.config.js""App.js"],</span>"compileOnSave": <span style="color: #0000ff"&gt;false</span><span style="color: #000000"&gt;

}

 7.安装react-native-scripts

yarn add react-native-scripts

8.将package.json中的"scripts"配置清空,并将以下代码替换

 

"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 "

9.将package.json中的"main"配置清空替换为以下代码

"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js"

10.将App.js中代码清空,并将以下代码替换

import App from './build/App' App;

11.创建一个src文件夹,将babel.config.js文件放在src文件夹下,同时在src文件夹下创建一个App.tsx文件,App.tsx文件中代码如下

import React,{ Component } from "react""react-native"export <span style="color: #0000ff">default<span style="color: #000000"> class App extends Component {
render() {
<span style="color: #0000ff">return<span style="color: #000000">(

红牛维生素功能饮料

12.执行yarn buildAndStart即可

  

  

相关文章

一、前言 在组件方面react和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...