Skip to content

[Bug]: generatePath doesn't work with optional params (v6.5.0-pre.1) #9736

@jesperjohansson

Description

@jesperjohansson

What version of React Router are you using?

6.5.0-pre.1

Steps to Reproduce

Reproduction demo: https://stackblitz.com/edit/github-kmv8on-hr7pvh?file=src/App.tsx

Attempt 1, path becomes /food/snacks/fruit/apple?/apples (with the ?) and wrong page is shown

<Link
  to={generatePath('food/:foodName/fruit/:fruitName?/apples', {
    foodName: 'snacks',
    fruitName: 'apple',
  })}
>
  Apples (generatePath)
</Link>

Attempt 2, throws error Missing ":fruitName" param

<Link
  to={generatePath('food/:foodName/fruit/:fruitName?/apples', {
    foodName: 'snacks',
    'fruitName?': 'apple',
  })}
>
  Apples (generatePath)
</Link>

Expected Behavior

Should had returned /food/snacks/fruit/apple/apples

Actual Behavior

Returns /food/snacks/fruit/apple?/apples

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions