wai*_*ira 1 javascript reactjs react-hooks
我是 React-Js 的新手,我有一个注册页面,我想向我的后端发送 Post 请求。UseState 正在工作,其他一切都正常,我的方法是 handleSubmit 。如何通过使用 axios 或 fetch 从这里使用 useEffect ?
export default function SignUp() {
const [username, setUsername] = useState('');
const [email, setEmail] = useState('');
const [passowrd, setPassowrd] = useState('');
const handleSubmit = e => {
e.preventDefault();
}
return (
<form className={classes.form} noValidate onSubmit={handleSubmit}>
</>
)
Run Code Online (Sandbox Code Playgroud)
小智 5
您不需要使用 useEffect Hook,因为您没有在组件加载/更改时执行它。而是使用 async/promise 和函数handleSubmit
export default function SignUp() {
const [username, setUsername] = useState('');
const [email, setEmail] = useState('');
const [passowrd, setPassowrd] = useState('');
const handleSubmit = async () =>{
const res = await axios.post('YOUR BACKEND',{
username,email,password
})
}
//remember to handle change for form inputs
return (
<form className={classes.form} noValidate onSubmit={handleSubmit}>
</>
)Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2422 次 |
| 最近记录: |