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