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

前端 未结 11 1013
耶瑟儿~
耶瑟儿~ 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:57

    You should avoid ref="googleInput" as it is now considered legacy. You should instead declare

    ref={(googleInput) => { this.googleInput = googleInput }}
    

    Inside of your handler, you can use this.googleInput to reference the element.

    Then inside of your submitForm function, you can obtain the text value with this.googleInput._getText()

    String refs are legacy https://facebook.github.io/react/docs/refs-and-the-dom.html

    If you worked with React before, you might be familiar with an older API where the ref attribute is a string, like "textInput", and the DOM node is accessed as this.refs.textInput. We advise against it because string refs have some issues, are considered legacy, and are likely to be removed in one of the future releases. If you're currently using this.refs.textInput to access refs, we recommend the callback pattern instead.

    Edit

    From React 16.3, the format for creating refs are:

    class Component extends React.Component 
    {
            constructor() 
            {
                this.googleInput = React.createRef();
            }
    
            render() 
            {
                return 
                (
                    
    {/* Details */}
    ); } }

提交回复
热议问题