在 React 中使用 Typescript
前言
用 Typescript 写 React 可比写 Vue 舒服太多了,React 对 ts 的支持可谓天生搭档,如果要用 ts 重构项目,不像 Vue 对项目破坏性极大,React 可以相对轻松的实现重构。
顺便一提:全局安装的 create-react-app 现已无法再下载完整的 React 项目模版,必须先 npm uninstall -g create-react-app 移除命令 再 npx create-react-app demo 下载模版,参考 create-react-app 官方github
主要步骤
1. 生成一个全新的 ts + react 的模版 可直接使用指令:npx create-react-app demo --typescript,注意在未来的版本中该指令会被替换为 npx create-react-app demo --template typescript,该模版包含了全套正常运行 React 所需的包和配置,无需再额外手动安装 typescript 等,其中还包含了自动化测试文件以及PWA所需文件等,可自行根据需求增删。
如在已有项目中使用typescript,需要手动安装 typescript @types/react @types/react-dom(使用@types/前缀表示我们额外要获取React和React-DOM的声明文件),然后在根目录下创建一个 tsconfig.json 文件,改后缀为 .tsx
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
"src"
]
}
2. 使用各种第三方库,如路由库 react-router-dom(需要额外安装声明文件@types/react-router-dom)、状态管理库 react-redux(需要额外安装声明文件@types/react-redux)、axios、在typescript中使用antd等。
基本使用方法
1. 类组件写法
import React from 'react'
interface Props {
endDate: string,
timeout: any
}
interface State {
now: any
}
let timer: any = null
class CountDown extends React.Component<Props, State>{
readonly state: Readonly<State> = {
now: moment()
}
componentDidMount(){
timer = setInterval((): void => {
this.setState({
now: moment()
})
}, 1000)
}
componentWillUnmount(){
clearInterval(timer)
}
get countDown(){ //类似 vue 中的计算属性
return (endDate: string): string => {
//...
}
}
}
render(): any{
return (
......
)
}
}
export default CountDown
2. 函数组件写法
const App: React.FC<Prop> = (prop) => {
return ()
}
在 React 中使用 Typescript的更多相关文章
- 在React中使用Typescript的实践问题总结
1.布尔值的大小写问题: 声明变量类型的时候,使用小写boolean 2. 对于从父组件传递过来的函数,子组件在模版中调用时,如果采用原来的写法,会报错: 改变写法后是如下这样,如果有参数和函数返回值 ...
- react中使用typescript时,error: Property 'setState' does not exist on type 'Home'
问题描述: 我在react中用typescript时,定义一个Home组件,然后在组件里用setState时会有这样一个报错:(如图)Property 'setState' does not exis ...
- 深入浅出TypeScript(5)- 在React项目中使用TypeScript
前言 在第二小节中,我们讨论了利用TypeScript创建Web项目的实现,在本下节,我们讨论一下如何结合React创建一个具备TypeScript类型的应用项目. 准备 Webpack配置在第二小节 ...
- React项目中应用TypeScript
一.前言 单独的使用typescript 并不会导致学习成本很高,但是绝大部分前端开发者的项目都是依赖于框架的 例如和vue.react 这些框架结合使用的时候,会有一定的门槛 使用 TypeScri ...
- Immutable 详解及 React 中实践
本文转自:https://github.com/camsong/blog/issues/3 Shared mutable state is the root of all evil(共享的可变状态是万 ...
- 在ReactNative中使用Typescript
在ReactNative中使用Typescript 少侠放心,跟着我的这个步骤走,保你完美在RN项目中使用Typescript,废话不多说,走你 1.全局安装create-react-native-a ...
- React 中的 onInput/onChange
参考链接:https://stackoverflow.com/questions/38256332/in-react-whats-the-difference-between-onchange-and ...
- 解读vue-server-renderer源码并在react中的实现
前言 在博客开发的过程中,有这样一个需求想解决,就是在SSR开发环境中,服务端的代码是是直接通过webpack打包成文件(因为里面包含同构的代码,就是服务端与客户端共享前端的组件代码),写到磁盘里 ...
- Vue项目中应用TypeScript
一.前言 与如何在React项目中应用TypeScript类似 在VUE项目中应用typescript,我们需要引入一个库vue-property-decorator, 其是基于vue-class-c ...
随机推荐
- centos系统重装python或yum 报There was a problem importing one of the Python modules required to run yum. The error leading to this problem was:错误
sudo vim /usr/bin/yum #修个python所在的路径,例如 #/usr/local/bin/python2.6 或 /usr/local/bin/python2.7要原本你的系统原 ...
- matplotlib如何显示中文
问题:matplotlib不能渲染中文 想设定为中文字体,网上搜索的方法几乎都是下面这样,已经把字体拷贝到了程序目录下了,然而并没有生效 plt.rcParams [ font.sans-serif' ...
- 解决intellij idea新建maven项目,加载archetype慢的问题
File->settings 在VM Options内输入 -DarchetypeCatalog=internal 重启idea
- Windows环境下Nginx配置本地虚拟域名
进入conf文件夹,新建servers文件夹: 将内部的server配置段提取单独放在一个文件里,存到了conf/servers下,以方便配置多个虚拟主机. 并在nginx.conf里http配置段内 ...
- K8S 容器挂载Windows 文件共享目录,访问出现Permission denied的解决办法
K8S Pod,Docker 容器挂载Windows主机目录,访问相应的文件出现Premission denied的权限访问问题 Windows远程共享目录为//192.168.61.115/huas ...
- StackExchange.Redis 之 Set集合 类型示例
话不多说直接上代码: // set添加单个元素 stopwatch.Start(); "); stopwatch.Stop(); Console.WriteLine("set添加单 ...
- VSTO开发指南(VB2013版) 第四章 Excel编程
实例1:处理NewWorkbook和WorkSheet事件的控制台程序 书本第70页 程序清单 4.1 处理NewWorkbook和WorkSheet事件的控制台程序 Imports Excel = ...
- Android中使用getDrawable时提示:Call requires API level 21(current min is 15)
场景 在通过getDrawable方法获取照片资源时提示: Call requires API level 21(current min is 15) 注: 博客: https://blog.csdn ...
- MySQL服务器的运维与优化
MySQL运维 安装数据库 配置本地yum源,将gpmall-repo文件上传至/opt目录 创建yum.repo文件 安装mariadb服务 # yum install -y mariadb mar ...
- Python 编程入门(1):基本数据类型
以下所有例子都基于最新版本的 Python,为了便于消化,每一篇都尽量短小精悍,希望你能尽力去掌握 Python 编程的「概念」,可以的话去动手试一下这些例子(就算目前还没完全搞懂),加深理解. 程序 ...