Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useSubmit must be used within a data router. (React Router 7) #127

Open
mjjabarullah opened this issue Nov 24, 2024 · 33 comments
Open

useSubmit must be used within a data router. (React Router 7) #127

mjjabarullah opened this issue Nov 24, 2024 · 33 comments

Comments

@mjjabarullah
Copy link

When reloading the page in browser. Application error occur
image

@AlemTuzlak
Copy link
Contributor

This usually happens when you have multiple react router versions in your project, check if you have any remix dependencies alongside your react-router

@mjjabarullah
Copy link
Author

I have checked there is no remix dependencies.
package.json file contains
{
"name": "@jchat/remix-app",
"private": true,
"sideEffects": false,
"type": "module",
"scripts": {
"build": "react-router build",
"dev": "react-router --host",
"lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .",
"start": "react-router-serve ./build/server/index.js",
"typecheck": "react-router typegen && tsc"
},
"dependencies": {
"@hookform/resolvers": "^3.9.0",
"@radix-ui/react-accordion": "^1.2.1",
"@radix-ui/react-avatar": "^1.1.1",
"@radix-ui/react-collapsible": "^1.1.1",
"@radix-ui/react-dialog": "^1.1.2",
"@radix-ui/react-dropdown-menu": "^2.1.2",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-popover": "^1.1.2",
"@radix-ui/react-scroll-area": "^1.2.0",
"@radix-ui/react-select": "^2.1.2",
"@radix-ui/react-separator": "^1.1.0",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-tooltip": "^1.1.3",
"@radix-ui/react-visually-hidden": "^1.1.0",
"@rajesh896/broprint.js": "^2.1.1",
"@react-router/node": "^7.0.1",
"@react-router/serve": "^7.0.1",
"@tanstack/react-query": "^5.59.20",
"axios": "^1.7.7",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"date-fns": "^4.1.0",
"isbot": "^4.1.0",
"jotai": "^2.10.1",
"lucide-react": "^0.453.0",
"react": "^18.2.0",
"react-day-picker": "8.10.1",
"react-device-detect": "^2.2.3",
"react-dom": "^18.2.0",
"react-hook-form": "^7.53.2",
"react-hot-toast": "^2.4.1",
"react-icons": "^5.3.0",
"react-router": "^7.0.1",
"redaxios": "^0.5.1",
"remix-hook-form": "^6.0.0",
"tailwind-merge": "^2.5.4",
"tailwindcss-animate": "^1.0.7",
"usehooks-ts": "^3.1.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@react-router/dev": "^7.0.1",
"@tanstack/eslint-plugin-query": "^5.60.1",
"@types/node": "^22.9.3",
"@types/react": "^18.2.20",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"autoprefixer": "^10.4.19",
"eslint": "^8.38.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jsx-a11y": "^6.7.1",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"postcss": "^8.4.38",
"prettier": "^3.3.3",
"prettier-plugin-tailwindcss": "^0.6.8",
"tailwind-scrollbar": "^3.1.0",
"tailwindcss": "^3.4.4",
"typescript": "^5.1.6",
"vite": "^5.1.0",
"vite-tsconfig-paths": "^4.2.1"
},
"engines": {
"node": ">=20.0.0"
}
}

Screen.Recording.2024-11-24.154907.mp4

@AlemTuzlak
Copy link
Contributor

Could you create a minimum repro? I'm not sure why this would happen

@mjjabarullah
Copy link
Author

const form = useRemixForm({
mode: "onSubmit",
resolver,
defaultValues: {
name: undefined,
password: undefined,
siteId: site?.id,
},
reValidateMode: "onChange",
});

when i comment this code and reload the page is working good. there is no error.

@AlemTuzlak
Copy link
Contributor

I'll try it out

@stilyng94
Copy link

@mjjabarullah have you found any solution?

@mjjabarullah
Copy link
Author

mjjabarullah commented Nov 25, 2024

@stilyng94 Not yet

@AlemTuzlak
Copy link
Contributor

I'm looking into this tomorrow!

@AlemTuzlak
Copy link
Contributor

AlemTuzlak commented Nov 26, 2024

I've pinpointed the issue to the <ServerRouter> from react-router, I'll communicate with the remix team to figure out the root cause as it's not on my side it seems. I'll get back to you asap!

A workaround for now is to wrap forms with but you probably don't want to do that as it skips server side rendering

@AlemTuzlak
Copy link
Contributor

remix-run/react-router#12401
I have filed an issue on react-router, also talked to the team, they are off for the holidays, so please be patient!

@mzulfanw
Copy link

mzulfanw commented Dec 1, 2024

i got same issue in here

@AlemTuzlak
Copy link
Contributor

AlemTuzlak commented Dec 3, 2024

It's an issue on react-router side, they released v7.0.2 that should fix it!

@hauptrolle
Copy link

It is still broken for me even in v7.0.2 😢

@AlemTuzlak
Copy link
Contributor

did you upgrade ALL the react-router packages @hauptrolle ? I tried it out myself and it works now

@akoenig
Copy link

akoenig commented Dec 3, 2024

I can also confirm that it works with v7.0.2. Thanks a lot @AlemTuzlak for taking care of it.

@hauptrolle
Copy link

Are u guys on vite6 or vite5 ?

@AlemTuzlak
Copy link
Contributor

Vite 5, vite 6 doesn't work yet with react-router from what I've heard and seen on their discord

@corygibbons
Copy link

@hauptrolle might be your node version. The fix wasn't working for me on node v23, but is working on v21 and v22. 🤷‍♂️

@hauptrolle
Copy link

@corygibbons Hmm I am on node v22.11.0

@AlemTuzlak
Copy link
Contributor

@hauptrolle can you post your package json

@hauptrolle
Copy link

{
  "name": "frontend",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "react-router build",
    "dev": "react-router dev",
    "start": "react-router-serve ./build/server/index.js",
    "typecheck": "react-router typegen && safe-routes typegen && tsc --build --noEmit",
    "prepare": "husky"
  },
  "dependencies": {
    "@hookform/resolvers": "3.9.1",
    "@opentelemetry/sdk-node": "0.55.0",
    "@radix-ui/react-label": "2.1.0",
    "@radix-ui/react-slot": "1.1.0",
    "@react-router/node": "7.0.2",
    "@react-router/serve": "7.0.2",
    "class-variance-authority": "0.7.1",
    "clsx": "2.1.1",
    "ioredis": "5.4.1",
    "isbot": "5.1.17",
    "lucide-react": "0.464.0",
    "react": "18.3.1",
    "react-dom": "18.3.1",
    "react-hook-form": "7.53.2",
    "react-router": "7.0.2",
    "remix-hook-form": "6.0.0",
    "safe-routes": "1.0.0",
    "sha256": "0.2.0",
    "tailwind-merge": "2.5.5",
    "tailwindcss-animate": "1.0.7",
    "zod": "3.23.8"
  },
  "devDependencies": {
    "@commitlint/cli": "19.6.0",
    "@commitlint/config-conventional": "19.6.0",
    "@eslint/js": "9.16.0",
    "@react-router/dev": "7.0.2",
    "@trivago/prettier-plugin-sort-imports": "4.3.0",
    "@types/node": "20",
    "@types/react": "18.3.12",
    "@types/react-dom": "18.3.1",
    "@types/sha256": "0.2.2",
    "autoprefixer": "10.4.20",
    "eslint": "9.16.0",
    "husky": "9.1.7",
    "postcss": "8.4.49",
    "prettier": "3.4.1",
    "prettier-plugin-tailwindcss": "0.6.9",
    "tailwindcss": "3.4.15",
    "typescript": "5.7.2",
    "typescript-eslint": "8.17.0",
    "vite": "5.4.11",
    "vite-tsconfig-paths": "5.1.3"
  }
}

@hauptrolle
Copy link

hauptrolle commented Dec 3, 2024

Hot-reloading removes the error. But after a hard page reload is appears again.

@AlemTuzlak
Copy link
Contributor

Can you maybe try clearing out node modules and removing the lock and installing again, maybe there's an issue there

@hauptrolle
Copy link

Did it already multiple times. Still same error. package-lock looks also good. All react-router packages are on 7.0.2. But if it works for you, it looks like an error on my side ...

@mjjabarullah
Copy link
Author

mjjabarullah commented Dec 3, 2024

After upgrading react-router package to 7.0.2. It is working for me. Thanks for your support @AlemTuzlak

@atarbalouti
Copy link

Did it already multiple times. Still same error. package-lock looks also good. All react-router packages are on 7.0.2. But if it works for you, it looks like an error on my side ...

Maybe try to use node v22 or lower, for me this was also the issue that I used node v23.

@mzulfanw
Copy link

mzulfanw commented Dec 3, 2024

remix-run/react-router#12401

still got the error, i following your guide

@hauptrolle
Copy link

Good to know that I am not the only one :)

@hauptrolle
Copy link

Even after an clean install like in your reproduction @AlemTuzlak I still get this error. On node v22.12.0 and react-router v.7.0.2. Did you manage to fix this @mzulfanw ?

@AlemTuzlak
Copy link
Contributor

This is something that needs to be fixed on react-router side, I have nothing to do with the cause of it. Our best bet to get this working for everyone is to write on the linked issue on their side to get visibility on it.

@mzulfanw
Copy link

mzulfanw commented Dec 5, 2024

@hauptrolle I didn't manage to fix the problem

@AlemTuzlak
Copy link
Contributor

remix-run/react-router#12475
here is another issue on top of the one you guys have been having, seems that it only works on some node versions.

@AlemTuzlak AlemTuzlak reopened this Dec 6, 2024
@cemalokten
Copy link

cemalokten commented Dec 9, 2024

I was having the same issue, but realised that the node version was only being set in the terminal window where I was running nvm. I ran nvm use 21 in the terminal window where I was running the project and it worked, I didn't realise nvm doesn't persist across the terminal windows.

n.b Only node 21 works, node 22 still returns the same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants