Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why react router v6 useParams returns object with properties possibly 'undefined'?

Why react router v6 useParams returns object with properties possibly 'undefined'?

In my code below, my IDE indicates const slug: string | undefined.

const { slug } = useParams<"slug">();

It is because of the Params type definition:

/**
 * The parameters that were parsed from the URL path.
 */
export type Params<Key extends string = string> = {
  readonly [key in Key]: string | undefined;
};

But why is not Params defined like this (without the | undefined):

export type Params<Key extends string = string> = {
  readonly [key in Key]: string;
};

If a route with a parameter matches the URL then the parameter cannot be undefined. So, is there any case when useParams returns with an undefined param?

like image 900
soliz Avatar asked Nov 16 '21 15:11

soliz


People also ask

How do you use useParams in react dom v6 router?

Using react-router-dom v6import { useParams } from "react-router-dom"; const Something = (props) => { let { id } = useParams(); useEffect(() => { console. log(`/something/${id}`); },[]); // ..... } A quick and exact solution. Thanks for the details.

How do I use useParams in typescript?

In order to useParams you need to implement a generic for useParams. Building on my example above, I need to type the id . type QuizParams = { id: string; }; // In order to implement that, I'd apply my type to the hook when calling it. const { id } = useParams<QuizParams>();

Is react router dom v6 stable?

A Quick Overview of React Router v6 It first launched in an alpha version in early 2021 and is now in a stable release. It has created a lot of buzz in the React community after its release.

How do I import Usenavigate into react?

Step 1: To start with, create a React application using the following command: npx create-react-app <project_name>; Step 2: Install the latest version of react-router-dom in the React application by the following. Project Structure: Create a folder named components in the src folder and add files Home.

Does react router V6 have a usesearchparams hook?

If you then had other components that updated other url parameters like filters and so on it still kept my "q"-parameter intact. In React router v6 they expose a hook called useSearchParams which is great and it was really missing something like that from v5.

How to access the param value inside a users component in react?

Now, we can access the :id param value inside a Users component by using the useParams () hook. In the above code, we first imported the useParams () hook from the react-router-dom package.

What is the useparams hook in react?

useParams () hook. The useParams () hook helps us to access the URL parameters from a current route. Example: Consider, we have a route like this in our react app. <Route path="/users/:id" component={Users} />. Now, we can access the :id param value inside a Users component by using the useParams () hook. Users.js.

What are the features of react-router-Dom?

It includes features like relative routing and linking, automatic route ranking, nested routes, and layouts. The last component from react-router-dom required is called Route and is responsible for rendering the UI of a React component. It has a prop called path which always matches the current URL of the application.


3 Answers

I believe this is a questionable design choice. React Router 6 doesn't support optional parameters, so there is no apparent reason, but even if it did support, it should be responsibility of the user to determine which parameters are optional.

One way to go around it is:

export type MyParams = {
  a: string;
  b: string;
  c: string;
};

const { a, b, c } = useParams<keyof MyParams>() as MyParams;
like image 112
André Pena Avatar answered Nov 10 '22 17:11

André Pena


Route params can be possibly undefined if the path has no defined parameters.

Example: "/path/to/page"

There are no route match params to access.

If you were to attempt to access a param

const { id } = useParams();

then id is of course, undefined.

like image 39
Drew Reese Avatar answered Nov 10 '22 19:11

Drew Reese


One workaround is to extend module and change the desired type.

Create src/react-router-dom.d.ts file with content:

import 'react-router-dom';

declare module 'react-router-dom' {
  export declare function useParams<
    ParamsOrKey extends string | Record<string, string | undefined> = string,
  >(): { [key in ParamsOrKey]: string };
}
like image 42
Uros Lazarevic Avatar answered Nov 10 '22 19:11

Uros Lazarevic