无法使用 this.refs 获取输入类型的值…如何从输入类型获取该值
export class BusinessDetailsForm extends Component {
submitForm(data) {
console.log(this.refs.googleInput.value)
}
}
reder() {
return(
<form onSubmit={this.submitForm}>
<Field type="text"
name="location"
component={GoogleAutoComplete}
id="addressSearchBoxField"
ref="googleInput"
/>
</form>
)
}
}
原文由 Ashh 发布,翻译遵循 CC BY-SA 4.0 许可协议
你应该避免
ref="googleInput"
因为它现在被认为是遗留的。你应该声明在处理程序内部,您可以使用
this.googleInput
来引用该元素。然后在
submitForm
函数内部,您可以使用this.googleInput._getText()
获取文本值字符串引用是遗留的 https://facebook.github.io/react/docs/refs-and-the-dom.html
编辑
从 React 16.3 开始,创建 refs 的格式为: