[React] Write a stateful Component with the React useState Hook and TypeScript
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的更多相关文章
- [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 ...
- React.createClass和extends Component的区别
React.createClass和extends Component的区别主要在于: 语法区别 propType 和 getDefaultProps 状态的区别 this区别 Mixins 语法区别 ...
- React Native 中的component 的生命周期
React Native中的component跟Android中的activity,fragment等一样,存在生命周期,下面先给出component的生命周期图 getDefaultProps ob ...
- React 的 PureComponent Vs Component
一.它们几乎完全相同,但是PureComponent通过prop和state的浅比较来实现shouldComponentUpdate,某些情况下可以用PureComponent提升性能 1.所谓浅比较 ...
- React 源码剖析系列 - 不可思议的 react diff
简单点的重复利用已有的dom和其他REACT性能快的原理. key的作用和虚拟节点 目前,前端领域中 React 势头正盛,使用者众多却少有能够深入剖析内部实现机制和原理. 本系列文章希望通过剖析 ...
- [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 ...
- React躬行记(5)——React和DOM
React实现了一套与浏览器无关的DOM系统,包括元素渲染.节点查询.事件处理等机制. 一.ReactDOM 自React v0.14开始,官方将与DOM相关的操作从React中剥离,组成单独的rea ...
- 【React】383- React Fiber:深入理解 React reconciliation 算法
作者:Maxim Koretskyi 译文:Leiy https://indepth.dev/inside-fiber-in-depth-overview-of-the-new-reconciliat ...
- React教程:4 个 useState Hook 示例
摘要: React示例教程. 原文:快速了解 React Hooks 原理 译者:前端小智 到 React 16.8 目前为止,如果编写函数组件,然后遇到需要添加状态的情况,咱们就必须将组件转换为类组 ...
随机推荐
- MCU PWM DAC OP Voltage Output
- the difference between an embOS interrupt and a zero latency interrupt
the difference between an embOS interrupt and a zero latency interrupt is the interrupt priority lev ...
- 使用winrar自解压功能制作安装包
参考文献: bat脚本设置文件的只读属性:http://wenda.tianya.cn/question/0f484c28ffd8d4e9 bat脚本创建internet快捷方式:http://www ...
- oracle 两个逗号分割的字符串 如何判断是否其中有相同值
比如字段A: 'ab,cd,ef,gh'字段B: 'aa,bb,cc,dd' 没有相同值 字段A: 'ab,cd,ef,gh'字段B: 'aa,bb,cd,dd' 有相同值cd 1.CREATE OR ...
- [置顶] Linux下发布QT程序
Linux下发布QT程序 概述 无论在windows下还是在linux下,可执行程序的运行都依赖于相关的运行库,我们需要将依赖的库找到放到特定的位置,让可执行文件能够找到.在不知道可执行文件依赖哪些库 ...
- chrome浏览器调试报错:Failed to load resource: the server responsed width a status of 404 (Not Found)…http://127.0.0.1:5099/favicon.ico
chrome浏览器在调试的时候默认会查找根目录下的favicon.ico文件,如果不存在就会报错. 解决办法:F12,点击<top frame>左侧漏斗形状的filter,勾选上" ...
- 利用HTTP Cache来优化网站
原文地址: http://www.cnblogs.com/cocowool/archive/2011/08/22/2149929.html 对于网站来说,速度是第一位的.用户总是讨厌等待,面对加载的V ...
- 【k8s】搭建步骤
搭建步骤 基础概念:https://www.cnblogs.com/sxdcgaq8080/p/10640879.html ====================================== ...
- JS --- reduce()函数
定义: reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值.对空数组是不会执行回调函数的. 案例 计算数组总和 var num = [1,2,3,4,5 ...
- 实习医生风云第一至九季/全集Scrubs迅雷下载
本季看点:<实习医生风云>一批医学院的学生来到圣心医院开始他们的实习生涯,但是从第一天起就发现这里并不是想象中安详宁静的医学圣地,从医生到护士甚至门卫个个不同寻常.内科实习医生杰迪是个聪明 ...