How to get values from input types using this.refs in reactjs?

前端 未结 11 1237
耶瑟儿~
耶瑟儿~ 2021-02-03 21:23

Not able to get values of input type using this.refs... how to get that values from input type

   export class BusinessDetailsForm extends Component {
      subm         


        
11条回答
  •  滥情空心
    2021-02-03 21:39

    using ref={ inputRef => this.input = inputRef } is considered legacy now. In React 16.3 onwards, you can use the code below,

    class MyForm extends React.Component {
        constructor(props) {
            //...
            this.input = React.createRef();
        }
    
        handleSubmit(event) {
            alert('A name was submitted: ' + this.input.current.value);
            event.preventDefault();
        }
    
        render() {
            return (
                
    ); } }

    EDIT: thanks for the comment @stormwild

提交回复
热议问题