在 React 中使用 Typescript
<h2><span style="font-size: 18px; color: rgba(216, 14, 75, 1)">前言</span></h2><p><span style="font-size: 16px"> 用 Typescript 写 React 可比写 Vue 舒服太多了,React 对 ts 的支持可谓天生搭档,如果要用 ts 重构项目,不像 Vue 对项目破坏性极大,React 可以相对轻松的实现重构。</span></p>
<blockquote>
<p><span style="font-size: 16px">顺便一提:全局安装的 create-react-app 现<span style="color: rgba(216, 14, 75, 1)">已无法再下载完整的 React 项目模版</span>,<span style="text-decoration: underline; color: rgba(216, 14, 75, 1)">必须</span>先 npm uninstall -g create-react-app 移除命令 再 npx create-react-app demo 下载模版,参考 create-react-app 官方github</span></p>
</blockquote>
<h2><span style="font-size: 18px; color: rgba(216, 14, 75, 1)">主要步骤</span></h2>
<p><span style="font-size: 16px"> 1. 生成一个全新的 ts + react 的模版 可直接使用指令:<span style="color: rgba(216, 14, 75, 1)">npx create-react-app demo --typescript</span>,注意在未来的版本中该指令会被替换为 npx create-react-app demo --template typescript,该模版包含了全套正常运行 React 所需的包和配置,无需再额外手动安装 typescript 等,其中还包含了自动化测试文件以及PWA所需文件等,可自行根据需求增删。</span></p>
<blockquote>
<p><span style="font-size: 16px">如在已有项目中使用typescript,需要手动安装 typescript @types/react @types/react-dom(使用@types/前缀表示我们额外要获取React和React-DOM的声明文件),然后在根目录下创建一个 tsconfig.json 文件,改后缀为 .tsx</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 0, 0, 1)">{
</span>"compilerOptions"<span style="color: rgba(0, 0, 0, 1)">: {
</span>"target": "es5"<span style="color: rgba(0, 0, 0, 1)">,
</span>"lib"<span style="color: rgba(0, 0, 0, 1)">: [
</span>"dom"<span style="color: rgba(0, 0, 0, 1)">,
</span>"dom.iterable"<span style="color: rgba(0, 0, 0, 1)">,
</span>"esnext"<span style="color: rgba(0, 0, 0, 1)">
],
</span>"allowJs": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"skipLibCheck": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"esModuleInterop": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"allowSyntheticDefaultImports": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"strict": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"forceConsistentCasingInFileNames": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"module": "esnext"<span style="color: rgba(0, 0, 0, 1)">,
</span>"moduleResolution": "node"<span style="color: rgba(0, 0, 0, 1)">,
</span>"resolveJsonModule": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"isolatedModules": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"noEmit": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"jsx": "react"<span style="color: rgba(0, 0, 0, 1)">
},
</span>"include"<span style="color: rgba(0, 0, 0, 1)">: [
</span>"src"<span style="color: rgba(0, 0, 0, 1)">
]
}</span></span></pre>
</div>
</blockquote>
<p><span style="font-size: 16px"> 2. 使用各种第三方库,如路由库 react-router-dom(需要额外安装<span style="color: rgba(216, 14, 75, 1)">声明文件</span>@types/react-router-dom)、状态管理库 react-redux(需要额外安装声明文件@types/react-redux)、axios、在typescript中使用antd等。</span></p>
<h2><span style="font-size: 18px; color: rgba(216, 14, 75, 1)">基本使用方法</span></h2>
<p><span style="font-size: 16px"> 1. 类组件写法</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px">import React from 'react'<span style="color: rgba(0, 0, 0, 1)">
interface Props {
endDate: string,
timeout: any
}
interface State {
now: any
}
let timer: any </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">
class CountDown extends React.Component</span><Props, State><span style="color: rgba(0, 0, 0, 1)">{
readonly state: Readonly</span><State> =<span style="color: rgba(0, 0, 0, 1)"> {
now: moment()
}
componentDidMount(){
timer </span>= setInterval((): <span style="color: rgba(0, 0, 255, 1)">void</span> =><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({
now: moment()
})
}, </span>1000<span style="color: rgba(0, 0, 0, 1)">)
}
componentWillUnmount(){
clearInterval(timer)
}
get countDown(){ <span style="color: rgba(216, 14, 75, 1)">//类似 vue 中的计算属性
</span></span><span style="color: rgba(0, 0, 255, 1)">return</span> (endDate: string): string =><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">...</span>
<span style="color: rgba(0, 0, 0, 1)"> }
}
}
render(): any{
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
......
)
}
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> CountDown</span></pre>
</div>
<p><span style="font-size: 16px"> 2. 函数组件写法</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px">const App: React.FC<Prop> = (prop) =><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ()
}</span></span></pre>
</div><br><br>
来源:https://www.cnblogs.com/eightFlying/p/react_typescript.html
頁:
[1]