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. XML 高速入门总结

    XML已经学习完了一段时间了.一直感觉知识比較琐碎,没有去好好总结.事实上越琐碎的知识也越须要我们去好好 理一下.将知识串起来.争取变得不再琐碎.以下是我学完xml画的一张图. 以下对XML进行一下简 ...

  2. 教程:如何手动安装Xamarin与Xamarin for VisualStudio

    [2016/4/17更新:如果你下载后发现仍然需要付费才能编译Android/iOS APP,请到文章最下面更新Xamarin for VS和Xamarin Studio到最新的版本.Build201 ...

  3. Time Zones And Daylight Savings Time

    This page describes code for working with Time Zones and Daylight Savings Time. Neither VBA nor VB6 ...

  4. 平台升级至nginx+Tomcat9.0.1(Spring5.0.1+velocity2.0+quartz-2.3.0)+redis集群

    在公司部份应用上 使用了 Tomcat9.0.1 稳定性还可以,由于将公司的集群服务也升级为 Tomcat9.0.1,下面我们来谈一下改变: 1:logging.properties 支持日志最大天数 ...

  5. Asp.Net Mvc3.0(MEF依赖注入理论)

    前言 Managed Extensibility Framework(MEF)是.NET平台下的一个扩展性管理框架,它是一系列特性的集合,包括依赖注入(DI)等.MEF为开发人员提供了一个工具,让我们 ...

  6. 获得assets文件夹中文件内容

    /** * @param fileName * @return assets中文件的字符串 */ public String getFromAssets(Context context, String ...

  7. 记一个js中的map数据结构

    <html><body> <script type="text/javascript">let arr =[{demo1:123,demo2:& ...

  8. SQL文件的BOM问题导致的invalid character错误及解决

    最近在做数据的搬运工,将Oracle中的数据搬运到ES中,方案很成熟了,使用Logstash的jdbc-input执行SQL,然后将结果输出到ES中.这么简单的问题,在测试环境中测试也一帆风顺,可一上 ...

  9. 如果类型是dynamic的且其属性也是dynamic的

    在 MVC 中,如果尝试如下的编码: public ActionResult TeacherInfo(string courseId) {     var x = LearningBll.GetTea ...

  10. 在ubuntu中搜索文件或文件夹的方法

    版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/dcrmg/article/details/78000961 1. whereis+文件名 用于程序名的搜索 ...