How to use refs in React with Typescript

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

    class SelfFocusingInput extends React.Component<{ value: string, onChange: (value: string) => any }, {}>{
        ctrls: {
            input?: HTMLInputElement;
        } = {};
        render() {
            return (
                 this.ctrls.input = input}
                    value={this.props.value}
                    onChange={(e) => { this.props.onChange(this.ctrls.input.value) } }
                    />
            );
        }
        componentDidMount() {
            this.ctrls.input.focus();
        }
    }
    

    put them in an object

提交回复
热议问题