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

Add tanstack/react-router support. TanstackRouterAppProvider #4629

Open
nick-kang opened this issue Jan 23, 2025 · 1 comment
Open

Add tanstack/react-router support. TanstackRouterAppProvider #4629

nick-kang opened this issue Jan 23, 2025 · 1 comment
Labels
examples Relating to /examples good first issue Great for first contributions. Enable to learn the contribution process.

Comments

@nick-kang
Copy link

nick-kang commented Jan 23, 2025

Summary

Add TanstackRouterAppProvider similar to the react-router one.

Here is a sample implementation based off the react-router implementation. It's working on my app.

// TanstackRouterAppProvider.tsx

import { useLocation, useNavigate, useSearch } from "@tanstack/react-router";
import { AppProvider, type AppProviderProps, Navigate, Router } from "@toolpad/core";
import { useCallback, useMemo } from "react";

export function TanstackRouterAppProvider({ children, ...rest }: AppProviderProps): React.JSX.Element {
  const { pathname } = useLocation();
  const searchParams = useSearch({ strict: false });
  const navigate = useNavigate();

  const navigateImpl = useCallback<Navigate>(
    (url, { history = "auto" } = {}) => {
      if (history === "auto" || history === "push") {
        navigate({ to: url.toString() }).catch(console.error);
      } else if (history === "replace") {
        navigate({
          to: url.toString(),
          replace: true
        }).catch(console.error);
      } else {
        throw new Error(`Unknown history option: ${history}`);
      }
    },
    [navigate]
  );

  const routerImpl = useMemo<Router>(
    () => ({
      pathname,
      searchParams: new URLSearchParams(searchParams),
      navigate: navigateImpl
    }),
    [
      pathname,
      searchParams,
      navigateImpl
    ]
  );

  return (
    <AppProvider router={routerImpl} {...rest}>
      {children}
    </AppProvider>);
}

Examples

No response

Motivation

No response

Search keywords: tanstack/react-router tanstack/router router tanstack

@nick-kang nick-kang added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jan 23, 2025
@Janpot Janpot added good first issue Great for first contributions. Enable to learn the contribution process. examples Relating to /examples and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jan 23, 2025
@Janpot
Copy link
Member

Janpot commented Jan 23, 2025

I believe we could start with an example. We'd probably like to let the router adapter interface mature a bit before promoting it to an actual AppProvider. There are some changes planned to fix basepath issues in the current implementations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
examples Relating to /examples good first issue Great for first contributions. Enable to learn the contribution process.
Projects
None yet
Development

No branches or pull requests

2 participants