[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 目前为止,如果编写函数组件,然后遇到需要添加状态的情况,咱们就必须将组件转换为类组 ...
随机推荐
- HDU 4786 Fibonacci Tree (2013成都1006题)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 4741 Save Labman No.004 (2013杭州网络赛1004题,求三维空间异面直线的距离及最近点)
Save Labman No.004 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- delphi 主线程向子线程发送消息
while True do begin if not PeekMessage(msg,0,0,0,PM_REMOVE) then begin case MsgWaitForMultipleObject ...
- canvas使用1
画直线: ? 1 2 3 4 5 6 7 8 9 10 11 var c = document.getElementById("myCanvas"); //不要忘写document ...
- Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(2) 自己封装的Calendar接口
本章主要是收藏一些常用的类和接口,包括:万年历(农历.阳历节日.阴历节日).自定义的Calendar接口. 万年历 源码如下(ChineseCalendar.java): package com.vi ...
- C#编程(二十四)----------修饰符
修饰符 修饰符即应用于类型或成员的关键字.修饰符可以指定方法的可见性,如public或private,还可以指定一项的本质,如刚发的vritual或abstract. 可见性的修饰符 修饰符 应用于 ...
- ASP.NET MVC:@helper 不能调试
ASP.NET MVC 的 @helper 不能设置断点,当然我们可以将逻辑移动到扩展方法中,这里介绍另外一种方式,使用:System.Diagnostics.Debug.WriteLine,编程旅途 ...
- ibatis.net:第五天,QueryForObject
xml <statement id="LoadOrder" parameterClass="int" resultClass="Order&qu ...
- 大并发下TCP内存消耗优化小记(86万并发业务正常服务)
转自:http://blog.csdn.net/u010954257/article/details/54178160 最近在做一个大并发服务的测试(目前测到86万,当然有大量长连接,每天打的日志高到 ...
- 蓝精灵:寻找神秘村Smurfs: The Lost Village迅雷下载
蓝妹妹(黛米·洛瓦托 Demi Lovato 配音)发现了一张遗落的地图,由此引发精灵们对于神秘村庄真实性的猜想.于是,满怀好奇心的蓝妹妹与聪聪(丹尼·朴迪 Danny Pudi 配音).笨笨(杰克· ...