React 各种小技巧总结(缓慢更新...)

117 阅读1分钟

React 各种小技巧总结(缓慢更新...)

1.组件的props类型透传.

image.png

import React, { ComponentProps } from 'react';
import {  Button } from 'antd';
//此处即可取到Button按钮的props
type ButtonProps = ComponentProps<typeof Button>;
type MyButtonProps = Omit<ButtonProps,'onClick'>&{
    onClick:(params:number)=>void
}
function MyButton1(props:MyButtonProps){
    //此时onClick参数传入【string】异常提醒
    return <div onClick={()=>props.onClick('字符串')}></div>
}
function MyButton2(props:MyButtonProps){
    //此时onClick参数传入【number】无异常
    return <div onClick={()=>props.onClick(33)}></div>
}