我在 ReactJS 中从表单中获取值,但与此同时,当我单击提交按钮时,它给我错误 TypeError: Cannot read property 'value' of undefined
。有人可以检查我可能面临的问题。
谢谢
import React, { Component } from 'react'
import { getFunName } from './helpers';
class StorePicker extends Component {
myInput=React.createRef();
goToStore=event=> {
// 1- Stopping form from submitting
event.preventDefault();
// 2 - Getting value from Input
const storeName = this.myInput.value.value ;
// 3 - Change the page to /store/whatever-you-entered
this.props.history.push(`/store/${storeName}`)
}
render() {
return (
<form className="store-selector" onSubmit={this.goToStore}>
<h2>Please Enter Enter a Store</h2>
<input type="text" required placeholder="Store Name" ref={this.myInput} defaultValue={getFunName()}/>
<button type="submit">Visit Store</button>
</form>
)
}
}
export default StorePicker;
最佳答案
如果你使用 refs,它应该是这样的:
const storeName = this.myInput.current.value;
关于javascript - react : TypeError: Cannot read property 'value' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54925670/