• [React Typescript 2022] Use TypeScript to Type a React Class Component


    For older applications, it's likely that you have some Class components. TypeScript works a little differently with these. The React.Component class is a generic class and it takes the props type as its first type argument. We will write out an alias for our props that I'll pass in the type argument space for the class. Child classes can also implement their own various methods for overriding parent methods, we'll also want to explicitly type the props in our constructor.

    import * as React from "react";
    
    interface CounterProps {
        initialCount?: number;
        className?: string;
    }
    interface CounterState {
        count: number;
    }
    class Counter extends React.Component<CounterProps, CounterState> {
        state: CounterState = {
            count: this.props.initialCount ?? 0,
        };
    
        constructor(props: CounterProps) {
            super(props);
            this.increment = this.increment.bind(this);
            this.decrement = this.decrement.bind(this);
        }
    
        shouldComponentUpdate(nextProps: CounterProps, nextState: CounterState) {
            return shallowCompare(this, nextProps, nextState);
        }
    
        increment() {
            this.setState(({ count: prevCount }) => ({
                count: prevCount + 1,
            }));
        }
    
        decrement() {
            this.setState(({ count: prevCount }) => ({
                count: prevCount - 1,
            }));
        }
    
        render() {
            return (
                <div className={this.props.className}>
                    <button type="button" onClick={this.decrement} aria-label="Decrement">
                        -
                    </button>
                    <span>{this.state.count}</span>
                    <button type="button" onClick={this.increment} aria-label="Increment">
                        +
                    </button>
                </div>
            );
        }
    }
    
    export { Counter };
  • 相关阅读:
    sql server sa 用户 属性
    关于SQL执行顺序
    SQL server 数据库版本查询
    版本生成|Ext form输入框后加文字说明
    继承 tabpanel 对父类重新赋值 父类方法重写
    Ext.namespace() Ext命名空间介绍
    Ext.apply Ext.applyif 的理解
    下拉列表
    extjs表单FormPanel验证
    提高篇:第十三篇
  • 原文地址:https://www.cnblogs.com/Answer1215/p/15750753.html
Copyright © 2020-2023  润新知