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]: loader of Route running even if Route / Router is not rendered #11468

Closed
Robstei opened this issue Apr 17, 2024 · 2 comments
Closed

[Bug]: loader of Route running even if Route / Router is not rendered #11468

Robstei opened this issue Apr 17, 2024 · 2 comments
Labels

Comments

@Robstei
Copy link

Robstei commented Apr 17, 2024

What version of React Router are you using?

6.22.3

Steps to Reproduce

Define a route with a loader before using an early return which is supposed to not render the router(#11205 (comment), https://reactrouter.com/en/main/route/loader#loader).

Go to "/" and get redirected to "/redirect".

Reproduction:
https://stackblitz.com/edit/github-gde4bp?file=src%2Fapp.tsx

Expected Behavior

Since loaders are executet before a is rendered i would asume that the loader is not executed and no redirect would happen.

Actual Behavior

Loader gets executed and redirect does happen.

@Robstei Robstei added the bug label Apr 17, 2024
@timdorr
Copy link
Member

timdorr commented Apr 19, 2024

This isn't stopping the root route from rendering in any way (outside of your example having an early return statement). So, the / route matches and then that load redirects to /redirect as expected. I'm not seeing how this behavior is unexpected in your example.

@timdorr timdorr closed this as not planned Won't fix, can't repro, duplicate, stale Apr 19, 2024
@Robstei
Copy link
Author

Robstei commented Apr 21, 2024

Thanks for the fast response @Timdor

The issue might be flawed understanding of react. I thought "rendering an element" in regards to react means "adding the element to the virtual DOM".

Due to returning on line 29 I thought the <Routprovider/> and therefore the <Route/> elements are never "rendered".

  return 'router not rendered';
  return <RouterProvider router={router} fallbackElement={<p>Loading...</p>} />;

Does "rendering an element" just mean creating it i the sense that the following code "renders" a div?

const myDiv = <div/> 

In that case the creation of the element in the following code from the reproduction "renders" the route regardless of what the component acutally adds to the DOM, right?

  let router = createBrowserRouter(
    createRoutesFromChildren(
      <>
        <Route
          path="/"
          element={'home'}
          loader={() => {
            throw redirect('redirect');
          }}
        >
          home
        </Route>
        <Route path="/redirect" element={'redirect'}></Route>
      </>
    )
  );

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

No branches or pull requests

2 participants