I am wondering if this is a possible bug? I am using NextJS 13. I’m creating a Search component using jsx. In my function I am building the form and I have an onSubmit action in the < form > element. When the page is rendered to the browser, the onSubmit is not there.
When I put in the action, method attributes, they show up. I had even put in an attribute named ‘something’ and it showed up in the DOM. When I changed the ‘something’ attribute to ‘onSomething’ it did not show up.
Here is my Search.jsx file.
'use client';
import { useRouter } from "next/navigation";
import { useState } from "react";
function Search() {
const [search, setSearch] = useState('');
const router = useRouter();
const handleSearch = async (e) => {
e.preventDefault();
setSearch('');
router.push(`/search/${search}`);
}
return (
<div className="p-2">
<form onSubmit={handleSearch}> <-- This is not showing up in the dev tools DOM
<input
type="text"
className="mr-2 text-black"
value={search}
placeholder="EntertheSearchTerm"
onChange={(e) => setSearch(e.target.value)} />
<button type="button" className="btn bg-blue-500 text-white font-bold py-2 px-4 rounded-lg">Search</button>
</form>
</div>
)
}
export default Search
This is from the DOM on the browser:
<div class="p-2">
<form>
<input type="text" class="mr-2 text-black" placeholder="EntertheSearchTerm" value="">
<button type="button" class="btn bg-blue-500 text-white font-bold py-2 px-4 rounded-lg">Search Me</button>
</form>
</div>
I’m not getting any errors on the page or in the console. Any attribute with the ‘on’ in front of it is not being populated to the DOM in the dev tools.
Question
Is this a possible bug? Or am I missing something?
If it is a bug, I have not found any documentation for it.
I’m new to NextJS but I do not feel it is something that I’m missing.
2
Answers
No, it is not a bug. It may not documented in NextJS but it there in React Documentation because NextJS is a framework built on top of React
you can also read this
This answer explain the reason why react event handler is not rendered to DOM
This is normal. React JSX props map onto HTML DOM properties, not onto HTML attributes.
Event handlers are not bound using intrinsic event attributes (which are awful).
Your form has an
onSubmit
event handler which will fire when the form is submitted.Your form has a
<button type="button">
which is not a submit button and will not submit the form.Remove the
type
attribute. (submit
is the default type).