reactjs - Cannot type into input field in React
问题描述
I am struggling with my input field & haven't been able to find a working solution. I somehow cannot type anything into them. Any suggestions on how I could solve it?
Is there anything wrong with my onChange() that I am overseeing? I don't get any errors though. That's why it is so weird.
export default class SignIn extends Component {
constructor(props) {
super(props);
this.state = {
name: '',
email: '',
id: '',
};
}
handleChange = (e) => {
const { name, value } = e.target;
this.setState({
[name]: value
});
}
signup = (e) => {
e.preventDefault();
this.setState({
id: uuid()
}, () => {
axios.post('https://api.xyz.co/api/signup?id=' + this.state.id + '&email=' + this.state.email + '&name=' + this.state.name)
.then((result) => {
console.log("submitted mail to join community");
console.log('hi' + this.state.name)
this.jump();
})
.catch(err => {
console.log(err);
});
});
}
jump() {
this.props.history.push({
pathname: `/sceneries/8`,
state: {
name: this.state.name
},
})
}
render() {
return (
<div id="signIn">
<form action="" method="post" className="form" >
<input type="text" placeholder="Your first name" value={this.state.name} onChange={this.handleChange}/>
<input
type="email"
value={this.state.email}
onChange={this.handleChange}
placeholder="Your email"
/>
<button id="submitEmail" type="submit" onClick={this.signup}>
Join us
</button>
</form>
</div>
);
}
}
Thanks a lot in advance!
解决方案
您只是缺少元素name
上的属性<input>
:
您的render
方法应如下所示:
render() {
return (
<div id="signIn">
<form action="" method="post" className="form" >
<input
name="name" //name attribute here
type="text"
placeholder="Your first name"
value={this.state.name}
onChange={this.handleChange}
/>
<input
name="email" //name attribute here
type="email"
value={this.state.email}
onChange={this.handleChange}
placeholder="Your email"
/>
<button id="submitEmail" type="submit" onClick={this.signup}>
Join us
</button>
</form>
</div>
);
}
推荐阅读
- sql - 比较连续行中的值并在不同的列中显示新添加或删除的项目
- asp.net-core - 部署后身份服务器出现多个问题
- ruby-on-rails - 如何为具有 uuid 的用户进行 Ahoy gem 跟踪访问?
- python-3.x -
而 POST 请求 - asp.net-mvc - ASPNET MVC 3 上的 WindowsAuthentication 不起作用(IsAuthenticated 始终为假)
- image - Flutter:如何在图像上创建文本,可通过手柄 4 个点调整大小
- accessibility - 网络可访问性 - 在按钮悬停时播放音频
- c - 为什么我的数组的总和(使用指针计算)不正确?
- asp.net-core - 如何迭代表单元素,然后将它们传递给 ASP.NET Core 中的控制器?
- python - 在 Python 中读取 JSON 文件