我正在学习如何使用 React 并开始使用类并决定改用 Hooks。我相信我已经正确地绘制了所有内容,但是我非常不确定如何使用 useEffect 构建我的 axios.post 以处理多个用户输入。
import React, { useState, useEffect } from 'react'
import axios from 'axios'
const Signup = () => {
const [customerSignUp, setCustomerSignUp] = useState([
{ email: '', password: '', firstName: '', lastName: ''}
]);
const handleChange = (event) => {
setCustomerSignUp(event.target.value)
}
const handleSubmit = (e) => {
e.preventDefault()
console.log(e)
}
useEffect(() => {
axios.post('/api/Customer/SignUp', {
})
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.log(error)
})
}, [])
我只包含 lastName 来展示我如何使用 handleChange 事件处理程序来更改客户的状态。
return (
<div className="container">
<form className='white' onSubmit={handleSubmit}>
<h5 className="grey-text.text-darken-3">Sign Up With Email</h5>
<div className="input-field">
<label htmlFor="lastName">Last Name</label>
<input type="text" name="lastName" value={customerSignUp.lastName} onChange={handleChange} required />
</div>
<div className="input-field">
<button className="btn blue darken-3" type="submit">Sign Up</button>
</div>
</form>
</div>
);
}
export default Signup