每个状态的eventHandler。有什么办法可以做得更好?是的,它会像这样工作
import React, { Component } from 'react';class UserForm extends Component { constructor() { super(); this.state = { fname: '', lname: '', email: '', }; } onChange = (e) => { this.setState({ [e.target.name]: e.target.value }); } render() { const { fname, lname, email } = this.state; return ( <form> <input type="text" name="fname" value={fname} onChange={this.onChange} /> <input type="text" name="lname" value={lname} onChange={this.onChange} /> <input type="text" name="email" value={email} onChange={this.onChange} /> </form> ); }}
并且关于提交表单的问题,您的axios帖子将像这样工作
onSubmit = (e) => { e.preventDefault(); // get our form data out of state const { fname, lname, email } = this.state; axios.post('/', { fname, lname, email }) .then((result) => { //access the results here.... }); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)