I migrated my app from React to Next Js. The final part I'm confused on is removing/adding classes on click of an element.
Essentially, I have an input element (and some other elements) that is set to display: none by default; on click, it should switch to display: block. This was working in React, but is not working after migration. The actual click event does work when I put an alert() within the function.
PLEASE HELP!!!!
Here is the onClick function:
searchActive = () =>{
if (typeof document !== "undefined"){
const searchForm = document.getElementById(styles["search-form"]);
searchForm.classList.remove(styles["display-none"]);
document.getElementById(styles["search-input-box"]).focus();
document.getElementById(styles["search-icon-id"]).classList.add(styles["display-none"]);
document.getElementById(styles["close-icon-id"]).classList.remove(styles["display-none"]);
}
}
UPDATED: This method of using useState also throws a scope error.
Component Function:
searchActive = () =>{
const [add, setAdd] = useState("none");
useState("block");
}
JSX:
<input style={{display: add}} onClick = {searchActive}/>
add ? class : ""