Here we refactor a React TypeScript class component to a function component with a useState hook and discuss how props and state types can be modeled accordingly.

import * as React from "react";
import { render } from "react-dom"; import "./styles.css"; type ButtonProps = {
label?: string;
children: React.ReactNode;
};
type ButtonState = {
isOn: boolean;
};
function Button (props: ButtonProps) { const [state, setState] = React.useState<ButtonState>({
isOn: false
}); const toggle = () => setState({ isOn: !state.isOn }); const { children } = props;
const { isOn } = state;
const style = {
backgroundColor: isOn ? "red" : "green"
};
return (
<button style={style} onClick={toggle}>
{children(isOn)}
</button>
);
} Button.defaultProps = {
label: "Hello World!"
}; function App() {
return (
<Button>
{isOn => (isOn ? <div> Turn off</div> : <div> Turn on</div>)}
</Button>
);
} const rootElement = document.getElementById("root");
render(<App />, rootElement);

[React] Write a stateful Component with the React useState Hook and TypeScript的更多相关文章

  1. [React] Refactor a Stateful List Component to a Functional Component with React PowerPlug

    In this lesson we'll look at React PowerPlug's <List /> component by refactoring a normal clas ...

  2. React.createClass和extends Component的区别

    React.createClass和extends Component的区别主要在于: 语法区别 propType 和 getDefaultProps 状态的区别 this区别 Mixins 语法区别 ...

  3. React Native 中的component 的生命周期

    React Native中的component跟Android中的activity,fragment等一样,存在生命周期,下面先给出component的生命周期图 getDefaultProps ob ...

  4. React 的 PureComponent Vs Component

    一.它们几乎完全相同,但是PureComponent通过prop和state的浅比较来实现shouldComponentUpdate,某些情况下可以用PureComponent提升性能 1.所谓浅比较 ...

  5. React 源码剖析系列 - 不可思议的 react diff

      简单点的重复利用已有的dom和其他REACT性能快的原理. key的作用和虚拟节点 目前,前端领域中 React 势头正盛,使用者众多却少有能够深入剖析内部实现机制和原理. 本系列文章希望通过剖析 ...

  6. [React] Update State Based on Props using the Lifecycle Hook getDerivedStateFromProps in React16.3

    getDerivedStateFromProps is lifecycle hook introduced with React 16.3 and intended as a replacement ...

  7. React躬行记(5)——React和DOM

    React实现了一套与浏览器无关的DOM系统,包括元素渲染.节点查询.事件处理等机制. 一.ReactDOM 自React v0.14开始,官方将与DOM相关的操作从React中剥离,组成单独的rea ...

  8. 【React】383- React Fiber:深入理解 React reconciliation 算法

    作者:Maxim Koretskyi 译文:Leiy https://indepth.dev/inside-fiber-in-depth-overview-of-the-new-reconciliat ...

  9. React教程:4 个 useState Hook 示例

    摘要: React示例教程. 原文:快速了解 React Hooks 原理 译者:前端小智 到 React 16.8 目前为止,如果编写函数组件,然后遇到需要添加状态的情况,咱们就必须将组件转换为类组 ...

随机推荐

  1. Word中如何从某一页重新开始页码

  2. FFMPEG采集摄像头数据并切片为iPhone的HTTP Stream流

    一.Windows下面编译ffmpeg 首先需要解决的问题是:在windows下面编译 ffmpeg, 并让其支持dshow, 本人把ffmpeg编译成功了, 但是编译出来的ffmpeg不支持dsho ...

  3. lodash用法系列(5),链式

    Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...

  4. 在ASP.NET MVC中使用Knockout实践08,使用foreach绑定集合

    本篇体验使用 foreach 绑定一个Product集合. 首先使用构造创建一个View Model. var Product = function(data) { this.name = ko.ob ...

  5. 使用newScheduledThreadPool来模拟心跳机制

    (使用newScheduledThreadPool来模拟心跳机制) 1 public class HeartBeat { 2 public static void main(String[] args ...

  6. 咏南中间件支持TMS WEB CORE客户端

    咏南中间件支持TMS WEB CORE客户端 TMS WEB CORE是优秀的JS前端,搭配咏南中间件后端,可以进行快速的企业应用开发.

  7. 查看webservice服务下的所有方法和参数类型

    方法:直接在IE浏览器中输入webservice的地址,查看返回的XML数据即可. 效果如下:

  8. 关于spring session redis共享session的跨子域的处理

    安装完redis, spring端只要下面这两个bean配置上就可以用了 <?xml version="1.0" encoding="UTF-8"?> ...

  9. 转: MySQL5.7 ERROR 1142 (42000)问题

    1,mysql全库导入报错 [root@dev_121_21 ~]# mysql--socket=/usr/local/mysql/mysql.sock --default-character-set ...

  10. [Android Pro] 组件化:企业级大型项目必经之路

    cp : https://www.csdn.net/article/2011-02-11/291667 摘要:超过一年以上.活跃开发的项目往往到后期陷入了一些共性的问题: 构建速度慢,往往生成一次最终 ...