Reputation: 340
I am new in React-Js,I have a register page that i want to send a Post request to my backend . UseState is working andeverything else,my method is handleSubmit .How do is use useEffect from here either by using axios or fetch?
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}>
</>
)
Upvotes: 1
Views: 2648
Reputation: 139
You don't need to use the useEffect Hook, as you are not performing it on component load/change. instead use async/promise with function 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}>
</>
)
Upvotes: 5