I'm having a problem with a simple form registration submit. Here's the code:
import React from 'react';
import {register} from 'Util/api'
function Registration() {
const [email, setEmail] = React.useState("")
const [password, setPassword] = React.useState("")
const [passwordCheck, setPasswordCheck] = React.useState("")
const [error, setError] = React.useState("")
const register = event => {
event.stopPropagation()
event.preventDefault()
register(email, password, passwordCheck).then(res => {
console.log(res)
}).catch(error => {
console.log(error)
})
}
return (
<div>
<form onSubmit={register}>
<div>
<label>Email:
<input
type="text"
placeholder="Email"
value={email}
onChange={ev => setEmail(ev.target.value)}
/>
</label>
</div>
<div>
<label>Password:
<input
type="password"
placeholder="Password"
value={password}
onChange={ev => setPassword(ev.target.value)}
/>
</label>
</div>
<div>
<label>Repeat password:
<input
type="password"
placeholder="Repeat password"
value={passwordCheck}
onChange={ev => setPasswordCheck(ev.target.value)}
/>
</label>
</div>
<button type="submit" value="Submit">Register</button>
{error && (
<div>{error}</div>
)}
</form>
</div>
);
}
export default Registration
When I click the button "register" the console returns the error:
Registration.js:12 Uncaught TypeError: event.stopPropagation is not a function
Same thing happen with event.preventDefault if I delete that line. It looks very similar to the example in the doc here...
What's wrong with my code?