I am trying to set an event in useEffect:
useEffect(() => {
var input = document.getElementById("emailInput");
console.log(input); // null when reloading page!
input.addEventListener("keyup", function(event) {
console.log(event);
if (event.key === 'Enter')
onLogin();
});
}, []);
Whenever I reload the page, the call to input.addEventListener fails because the value of input is null. It is my understanding that useEffect runs after the component is rendered, so the input element with id="emailInput" should already exist by then. What am I missing?
UPDATE: I was rendering the element conditionally based on something assigned in useEffect. I did not show that part in the question, but the part not shown was the problem...