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

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

    I tried the answer above (https://stackoverflow.com/a/52269988/1978448) and found it only worked for me when I put the refs in the state, but not when I just made them properties of the component.

    Constructor:

    this.state.refs={
      fieldName1: React.createRef(),
      fieldName2: React.createRef()
    };
    

    and in my handleSubmit I create a payload object to post to my server like this:

    var payload = {
      fieldName1: this.state.refs.fieldName1.current.value,
      fieldName2: this.state.refs.fieldName2.current.value,
    }
    

提交回复
热议问题