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]: useParams type always adds undefined #11295

Closed
amir-beheshty opened this issue Feb 21, 2024 · 4 comments
Closed

[Bug]: useParams type always adds undefined #11295

amir-beheshty opened this issue Feb 21, 2024 · 4 comments
Labels

Comments

@amir-beheshty
Copy link

What version of React Router are you using?

6

Steps to Reproduce

  1. Run below code
  2. Notice the token type is string | undefined
  3. Similarly, token2 type is string | null | undefined

It seems that the Params type is extended to always include undefined.

(also, for some reason interface does not work!)

import React from 'react';
import {
  useParams,
  createBrowserRouter,
  RouterProvider,
} from 'react-router-dom';

type Params = {
  token: string;
  token2: string | null;
};

const Token = () => {
  const { token, token2 } = useParams<Params>();
  return <p>token: {token}</p>;
};

const router = createBrowserRouter([
  {
    path: '/:token',
    element: <Token />,
  },
]);

export const App = () => {
  return (
    <React.StrictMode>
      <RouterProvider router={router} />
    </React.StrictMode>
  );
};

Expected Behavior

The supplied type/interface for useParams should not be extended to include undefined.

Actual Behavior

The supplied type/interface for useParams is always extended to include undefined.

@hjonasson
Copy link

This is an interesting point. If token would be undefined that would mean your route wasn't /:token🤔 . I am curious to see what comes out of this.

Note that you should not expect to get token2 from your useParams hook as it is not defined on your route.

@amir-beheshty
Copy link
Author

Note that you should not expect to get token2 from your useParams hook as it is not defined on your route.

Correct. I used token2 to illustrate that token and token2 including undefined type is confusing. Clearly token2 should be undefined but token should always be string in the provided example.

@brophdawg11
Copy link
Contributor

This is expected because React Router cannot be sure in what context you're calling useParams - see this original PR for context: #8019.

It's worth noting that we are aware that React Router and Remix are not as type-safe/type-inferred as they could be and that's something we're actively thinking about when considering future Remix verisons/APIs. This would include Remix SPA mode - the bridge we're building to ensure React Router apps have a smooth path to get all the latest goodies as well.

@brophdawg11 brophdawg11 closed this as not planned Won't fix, can't repro, duplicate, stale Feb 28, 2024
@hjonasson
Copy link

Thanks @brophdawg11 . That's a good explanation, that useParams will only work as expected when used as expected which is very easy to mess up.

Love the SPA mode. I have always had to build the code splitting :D

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

3 participants