How to use refs in React with Typescript

前端 未结 15 1457
闹比i
闹比i 2020-11-28 05:03

I\'m using Typescript with React. I\'m having trouble understanding how to use refs so as to get static typing and intellisense with respect to the react nodes referenced by

15条回答
  •  旧巷少年郎
    2020-11-28 05:16

    One way (which I've been doing) is to setup manually :

    refs: {
        [string: string]: any;
        stepInput:any;
    }
    

    then you can even wrap this up in a nicer getter function (e.g. here):

    stepInput = (): HTMLInputElement => ReactDOM.findDOMNode(this.refs.stepInput);
    

提交回复
热议问题