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

[Bug]: pathless route throw no action method found #9284

Closed
nicolastoulemont opened this issue Sep 17, 2022 · 7 comments · Fixed by #9455
Closed

[Bug]: pathless route throw no action method found #9284

nicolastoulemont opened this issue Sep 17, 2022 · 7 comments · Fixed by #9455
Assignees
Labels

Comments

@nicolastoulemont
Copy link

nicolastoulemont commented Sep 17, 2022

What version of React Router are you using?

v6.4

Steps to Reproduce

Follow the react-router tutorial including the pathless route section

If implemented as suggested, the root action implemented earlier in the tutorial to create a contact will not work anymore.

Expected Behavior

The root action should work correctly or the tutorial should explain and provide a workaround.

Actual Behavior

React router will throw the following error :
react_devtools_backend.js:4026 ErrorResponse {status: 405, statusText: 'Method Not Allowed', data: 'No action found for [/]'}

Router configuration when the error happen:

const router = createBrowserRouter([
  {
    path: '/',
    element: <Root />,
    errorElement: <ErrorPage />,
    loader: rootLoader,
    action: rootAction,
    children: [
      {
        errorElement: <ErrorPage />, // <--- This seems to cause the error
        children: [
          { index: true, element: <Index /> },
          /* the rest of the routes */
        ],
      },
    ],
  },
])

Router configuration to remove the error and allow the action to be performed:

const router = createBrowserRouter([
  {
    path: '/',
    element: <Root />,
    errorElement: <ErrorPage />,
    loader: rootLoader,
    action: rootAction,
    children: [
      { index: true, element: <Index /> }, // <--- Removing it solves the issue
     /* the rest of the routes */
    ],
  },
])

Nb: the following router config will remove the error but prevent the action to be performed:

const router = createBrowserRouter([
  {
    path: '/',
    element: <Root />,
    errorElement: <ErrorPage />,
    loader: rootLoader,
    action: rootAction,
    children: [
      {
        errorElement: <ErrorPage />,
        action: () => {},  // <--- Prevent error throwing but also prevent the rootAction of being run
        children: [
          { index: true, element: <Index /> },
          /* the rest of the routes */
        ],
      },
    ],
  },
])
@nicolastoulemont nicolastoulemont changed the title [Bug]: Tutorial pathless route throw no action method found [Bug]: pathless route throw no action method found Sep 20, 2022
@rago4
Copy link

rago4 commented Sep 20, 2022

Moving rootAction to the children solves this issue.

const router = createBrowserRouter([
  {
    path: "/",
    element: <Root />,
    errorElement: <ErrorPage />,
    loader: rootLoader,
    children: [
      {
        errorElement: <ErrorPage />,
        action: rootAction, // 👈 move it here
        children: [
          // the rest of the routes
        ],
      },
    ],
  },
]);

@nicolastoulemont
Copy link
Author

Moving rootAction to the children solves this issue.

Yes indeed, but I would think that it kind break the semantics (a comprehension benefits) of defining the action & loader of a route within its given route object. WYDT ?

@brophdawg11
Copy link
Contributor

Thanks for the explanation! This looks like a bug as pathless layout routes should be ignored for form actions, and this should correctly find the root action. We'll get a fix in soon!

@brophdawg11 brophdawg11 self-assigned this Sep 20, 2022
@nicolastoulemont
Copy link
Author

Thanks for the explanation! This looks like a bug as pathless layout routes should be ignored for form actions, and this should correctly find the root action. We'll get a fix in soon!

Thanks !

@brophdawg11
Copy link
Contributor

Merged the fix for this - should be available in a 6.4.3 hopefully later this week or next week 👍

@nicolastoulemont
Copy link
Author

Thanks for your work @brophdawg11

@brophdawg11
Copy link
Contributor

👋 Just did a prerelease (6.4.3-pre.0) which should fix this if you want to give it a shot!

brophdawg11 pushed a commit that referenced this issue Apr 23, 2024
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants