1

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}>
    </>
  )
6
  • 2
    Why do you want to use useEffect? Isn't the trigger condition "When the form is submitted"? Commented Apr 13, 2021 at 17:13
  • also, please add handleSubmit Commented Apr 13, 2021 at 17:16
  • I was searching online and i saw a lot of guys using that ,i am open for any answer Commented Apr 13, 2021 at 17:16
  • is it mandatory to use useEffect for making api call ? you can make api call in handleSubmit function itself. Commented Apr 13, 2021 at 17:17
  • Exactly. useEffect is for reacting to state/props change Commented Apr 13, 2021 at 17:18

1 Answer 1

5

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}>
    </>
  )

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.