I am trying to make form validate in simple react app, but when I type something in input element and submit form it throw error can not read value of null, when that same input is empty getElementById return that input... I can not figure out what is wrong, I am new as a developer
import styles from './RegisterForm.module.css'
import { Link } from 'react-router-dom'
const RegisterForm = () => {
const name = document.getElementById('name');
const surname = document.getElementById('surname');
const company = document.getElementById('company');
const address = document.getElementById('address');
const city = document.getElementById('city');
const state = document.getElementById('state');
const zip = document.getElementById('zip');
const phone = document.getElementById('phone');
const email = document.getElementById('email');
const password = document.getElementById('password');
const confirm = document.getElementById('confirm');
const validation = e => {
e.preventDefault();
console.log(name);
if(name.value === '') {
name.className = styles.error;
name.focus();
}
}
return(
<div className ={styles.main}>
<div className className = {styles.subdiv}>
<div className = {styles.title}>First App</div>
<h1 className = {styles.welcome}>Welcome</h1>
<p className = {styles.createAccount}>Create your account</p>
<form id="myForm" type = "submit" className = {styles.form} onSubmit={validation}>
<input id="name" type="text" placeholder = "First name"/>
<input id="surname" type="text" placeholder = "Last name"/>
<input id="company" type="text" placeholder = "Company"/>
<input id="address" type="text" placeholder = "Address"/>
<input id="city" type="text" placeholder = "City"/>
<input id="state"type="text" placeholder = "State"/>
<input id="zip" type="text" placeholder = "Zip Code"/>
<input id="phone" type="number" placeholder = "Phone"/>
<input id="email" type="email" placeholder = "Email"/>
<input id="password" type="password" placeholder = "Enter your password"/>
<input id="confirm" type="password" placeholder = "Confirm your password"/>
<button className = {styles.button}>Register</button>
</form>
<div className = {styles.lastDiv}><p>Already have an account?</p><Link to="/"><a href="">Login!</a></Link></div>
</div>
</div>
)
}
export default RegisterForm;