Hilshire / blog

temporary blog
2 stars 0 forks source link

React Refs with TypeScript #16

Closed Hilshire closed 5 years ago

Hilshire commented 5 years ago

React Refs with TypeScript

工作中遇到的场景:ref 指向 antd 的 Form 组件

...
import { WrappedFormUtils } from 'antd/lib/form/Form';

export default class CyclePurchaseUserSearch extends React.Component<any, any> {
    xForm = React.createRef<WrappedFormUtils>()

    handleSubmit = () => {
           this.xForm.current!.validateFields((err) => {
                ...
            })
        }
    }

    render () {...}
}

写 ts 真的会越写约觉得自己菜...

Hilshire commented 5 years ago

混乱邪恶(但是很爽):

xform: any = React.createRef()