Next.js Discord

Discord Forum

Can't get form onSubmit to call the function I am giving it

Answered
Araucanian herring posted this in #help-forum
Open in Discord
Araucanian herringOP
I must be stupid or something. I have a simple page trying to log in using a form. I am trying to use react-hook-form which is basically irrelevant information, because the only thing that happens is my page refreshes with the input fields as query parameters. Its like nothing I pass as props to the form component does anything. Is there some kind of edge case thing I am dealing with that is causing this to happen? I don't understand.

<form onSubmit={(e) => {
    e.preventDefault();
    console.log(e);
  }}>
... formStuff
<button type="submit">submit</button>
</form>
Answered by Sussex Spaniel
Yea I just created a codesandbox, recreating the html you had listed, unsure what dependency the Input element comes from, I just changed it to input and removed the label attr. Here's a screenshot of the output:
View full answer

69 Replies

can you share your full onsubmit code... like what do you do after submission?
Araucanian herringOP
the above thing is all i do on submit. I just want to prevent default and console.log. That doesn't even work
and thats the only form in the whole page (that the button is nested in)?
also as you are using react-hook-form, you should make use of their wrapper: https://react-hook-form.com/docs/useform/handlesubmit
Araucanian herringOP
No other form.
and the react-hook-form stuff is irrelevant if i can't even prevent default
and console.log
im pretty sure they call prevent default...
Araucanian herringOP
i've removed react-hook-form completely just trying to get a console log
like i said, react-hook-form is irrelevant because I can't even get a form to console.log
Araucanian herringOP
sure. I've used react-hook-form quite a bit before.
im lost on the issue as i have used it prevent default many times with no issues...
and this is pages or app dir?
Araucanian herringOP
pages in the app dir
/auth/login
and im assuming a client component
Araucanian herringOP
yeah. its just in the page file with "use client"
well idk the error... it is [documented to work for react](https://react.dev/learn/responding-to-events#preventing-default-behavior>) and it works for me... so unless you have something really weird going on, idk...
can you try with nextjs canary version...?
Araucanian herringOP
<form
      onSubmit={(e) => {
        e.preventDefault();
        console.log(e);
      }}
    >
      <Input
        label="Email"
        id="email"
        type="email"
        placeholder="example@mail.com"
        onChange={() => {}}
      />
      <button type="submit">Send Password Reset Email</button>
    </form>
here is the full component.
this doesn't prevent default and doesn't console.log
and do you somehow have javascript disabled in your browser
Araucanian herringOP
just checked. enabled
if you put a useffect above the return and run alert or something to check...
i just want to see if somehow react isn't running the js on client
but if you can create a [minimal-reproduction-repository](https://nextjs-faq.com/minimal-reproduction-repository), then create it and see if the error still continues.... and then create an issue on github
Araucanian herringOP
well I removed everything from my app, and even just a page with an input with onChange={console.log} doesn't yield anything in the console
idk what the heck is happening
Araucanian herringOP
yeah no dice
i think you have to have js disabled...
there is no way that it isn't working...
like do you have an adblocker that is disabling it
Araucanian herringOP
i tried 2 different browsers and both have it enabled
😭
if you start a whole new repo with create-next-app and run that, is that working?
Araucanian herringOP
allowJS: true in my tsconfig
@Araucanian herring allowJS: true in my tsconfig
that doesn't mean anything here
it basicly just means it checks .js/.jsx files for types and not just .ts/.tsx
Araucanian herringOP
oh i see
Araucanian herringOP
well new project works. but why
you must have some special config 😭
Araucanian herringOP
i went through and made sure the configs match
try deleting .next folder and node_modules + update to latest of things and try again
Araucanian herringOP
idk how but that worked
thanks.
😵‍💫
ig some weird cache
i should have lead with that tbh
Araucanian herringOP
welp. it worked for like 5 minutes
nooooo
what occures now? no js events again
Araucanian herringOP
same kind of thing as before. no js seems to run
try deleting it again
lol but actually i have no idea
Araucanian herringOP
i tried just nuking .next this time
you may be having some weird decency dependency that is messing with things
@Araucanian herring i tried just nuking .next this time
yeah node_modules is usaly unnessary
Araucanian herringOP
dependency*?
"dependencies": {
    "@types/node": "20.6.0",
    "@types/react": "18.2.21",
    "@types/react-dom": "18.2.7",
    "autoprefixer": "10.4.15",
    "classnames": "^2.3.2",
    "eslint": "8.49.0",
    "eslint-config-next": "13.4.19",
    "firebase": "^10.3.1",
    "next": "13.4.19",
    "postcss": "8.4.29",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-hook-form": "^7.46.2",
    "tailwindcss": "3.3.3",
    "typescript": "5.2.2"
  },
  "devDependencies": {
    "@testing-library/jest-dom": "^6.1.3",
    "@testing-library/react": "^14.0.0",
    "@typescript-eslint/eslint-plugin": "^6.7.2",
    "encoding": "^0.1.13",
    "eslint-config-prettier": "^9.0.0",
    "jest": "^29.7.0"
  }
i don't think i am using anything that unique
hmmm
@Araucanian herring dependency*?
Sussex Spaniel
Yea I just created a codesandbox, recreating the html you had listed, unsure what dependency the Input element comes from, I just changed it to input and removed the label attr. Here's a screenshot of the output:
Answer
Sussex Spaniel
Also, here's a link to the codesandbox itself: https://codesandbox.io/s/smoosh-water-9x4hrk?file=/src/App.tsx
Might be as the original helper says, an odd bug, because as you can see, I basically reproduced it, but did not run into the bug. (I say basically because I didn't use the Input element but opted for the input element, although I doubt that had influence).
so your custom input component was the issue?
Ohh you didn't create it