I am looking to implement a redirection function in nextjs when users press a key.
There is a search input field where users can type and then hit the enter key to navigate to another page.
Here's what I have attempted:
const handleKeyPress = (e) => {
const ENTER_KEY_CODE = 13;
if (e.keyCode === ENTER_KEY_CODE) // perform history.push("/news");
console.log("Enter key pressed");
};
<input
onKeyPress={(e) => handleKeyPress(e)}
type="text"
name="search"
placeholder="Search by keywords"
className="p-4 md:p-6 w-full py-2 md:py-4 border-2 text-lg md:text-2xl xl:text-3xl border-gray-400 outline-none filosofia_italic bg-white placeholder-gray-400"
/>
Your assistance would be highly appreciated.