How to use refs in React with Typescript

前端 未结 15 1439
闹比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:15

    EDIT: This is no longer the right way to use refs with Typescript. Look at Jeff Bowen's answer and upvote it to increase its visibility.

    Found the answer to the problem. Use refs as below inside the class.

    refs: {
        [key: string]: (Element);
        stepInput: (HTMLInputElement);
    }
    

    Thanks @basarat for pointing in the right direction.

提交回复
热议问题