Hello Friends!
could someone help me to figure out whats the issue with my code? I created a Login Form with a transformation which I like to trigger with Javascript. Everything works fine with vanilla JS but when I try to use this code in React (Next JS) then I got this error:
import { useState } from 'react';
export default function LoginForm() {
// OPEN AND CLOSE LOGIN FORM
const [login, setLogin] = useState(false);
const toggleLogin = () => {
setLogin(!login);
};
// LOGIN FORM ANIMATION/MOVEMENT
if (typeof window === 'object') {
const signUpButton = document.getElementById('signUp');
const signInButton = document.getElementById('signIn');
const loginFormContainer = document.getElementById('login-form_wrapper');
signUpButton.addEventListener('click', () =>
loginFormContainer.classList.add('right-panel-active')
);
signInButton.addEventListener('click', () =>
loginFormContainer.classList.remove('right-panel-active')
);
}
return (
<>
<button className="btn-login" onClick={toggleLogin}>
Login
</button>
{login && (
<div className="login-form_container">
<div className="login-popup">
<div className="login-form_wrapper" id="login-form_wrapper">
<div className="form-container sign-up-container">
<form action="#">
<h1>Create Account</h1>
<p className="p-login-form">Start your journey with us!</p>
<input type="text" placeholder="Name" />
<input type="text" placeholder="Company" />
<input type="email" placeholder="Email" />
<input type="password" placeholder="Password" />
<input type="text" placeholder="Phone" />
<input type="text" placeholder="Country" />
<p className="p-login-form">
Once we have received your request, we will get in contact
with you soon.
</p>
<button className="btn-primary">Sign Up</button>
</form>
</div>
<div className="form-container sign-in-container">
<form action="#">
<h1>Welcome Back!</h1>
<p className="p-login-form">Login to your account</p>
<div className="input-container">
<input type="email" placeholder="Email" />
<input type="password" placeholder="Password" />
</div>
<a href="#" className="reset-password">
Forgot your password?
</a>
<button className="btn-primary">Login</button>
</form>
</div>
<div className="overlay-container">
<div className="overlay">
<div className="overlay-panel overlay-left">
<h1>Back to Login?</h1>
<p className="p-login-form-02">
Click here when you already have an account.
</p>
<button className="ghost" id="signIn">
Login
</button>
</div>
{/* OVERLAY RIGHT */}
<div className="overlay-panel overlay-right">
<button
className="btn-close-login-form"
onClick={toggleLogin}
>
CLOSE
</button>
<h1>Hello Friends!</h1>
<p className="p-login-form-02">
Enter your personal details and start your journey with
us.
</p>
<p>Let´s build something great!</p>
<button className="ghost" id="signUp">
Sign Up
</button>
</div>
</div>
</div>
</div>
</div>
</div>
)}
</>
);
}
Actually all I wanted to do is to convert Vanilla JS into a React (Next JS) App, but for whatever reason I got this:
TypeError: can't access property "addEventListener", signUpButton is null
But the SignUp/signIn ID is connected to the button. .
Can anyone see what I am doing wrong here??
Thanks a lot for help!
.
.
.
PS: If you need more information please let me know. I can also upload a repo if this is necessary to check whats wrong.
PPS: Marry Christmas to everyone!