Using React.findDOMNode in TypeScript

后端 未结 5 559
难免孤独
难免孤独 2021-02-04 16:24

I\'m following the React Tutorial and got stuck on how to use React.findDOMNode.

Here is my code:

export class CommentForm extends React.Com         


        
5条回答
  •  再見小時候
    2021-02-04 17:05

    None of the above solutions quite worked for me (and I think this might be a simpler solution). This is how I managed to get it to work in typescript (example using refs to focus the FormControl):

    Make sure you've imported ReactDom:

    import * as ReactDOM from 'react-dom';
    

    In your component:

    public focus():void {
        let input = ReactDOM.findDOMNode(this.refs["titleInput"]) as HTMLInputElement;
        input.focus();
    }
    
    render() {
        return (
            
                
            
        );
    }
    

提交回复
热议问题