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

Fix issues with contextual routing and index params #12003

Merged
merged 1 commit into from
Sep 17, 2024

Conversation

brophdawg11
Copy link
Contributor

Copy link

changeset-bot bot commented Sep 16, 2024

🦋 Changeset detected

Latest commit: 41100b9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
react-router-dom Patch
react-router Patch
@remix-run/router Patch
react-router-dom-v5-compat Patch
react-router-native Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@brophdawg11 brophdawg11 linked an issue Sep 16, 2024 that may be closed by this pull request
</Route>
),
{
window: getWindow("/parent?index&index=keep"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we have an ?index around from a prior submission, we were incorrectly letting that apply to a submit(data, { method: 'post' }) in the parent component and then it would try to submit to the child index route action and not the proper contextual parent route action

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was handled properly in useFormAction (so Form/fetcher.Form didn't have the bug) but when using useSubmit it goes straight to the router which does the path resolving, so we were missing the logic in router.navigate/router.fetch

</Route>
),
{
window: getWindow("/parent?index&index=keep"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Form/fetcher.Form did not have this issue since useFormAction handled it - but the logic in useFormActionto remove the ?index param was naive and removed all index params, not preserving user-specified ones. This PR also fixes that issue

expect(container.innerHTML).toMatch(/(:r2a:|:rp:),my-key/)
// React `useId()` results in something such as `:r2a:`, `:r2i:`,
// `:rt:`, or `:rp:` depending on `DataBrowserRouter`/`DataHashRouter`
expect(container.innerHTML).toMatch(/(:r[0-9]?[a-z]:),my-key/)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These internal react IDs seem to have changed with the new tests

params.delete("index");
path.search = params.toString() ? `?${params.toString()}` : "";
indexValues.filter((v) => v).forEach((v) => params.append("index", v));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preserve user-specified index params inside useFormAction

Comment on lines +4181 to +4187
// Remove existing ones when we're not
let params = new URLSearchParams(path.search);
let indexValues = params.getAll("index");
params.delete("index");
indexValues.filter((v) => v).forEach((v) => params.append("index", v));
let qs = params.toString();
path.search = qs ? `?${qs}` : "";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove ?index param when submitting to a non-index parent route

@brophdawg11 brophdawg11 merged commit 0389eba into v6 Sep 17, 2024
3 checks passed
@brophdawg11 brophdawg11 deleted the brophdawg11/fix-submission-index-param branch September 17, 2024 14:35
Copy link
Contributor

github-actions bot commented Oct 9, 2024

🤖 Hello there,

We just published version 6.27.0-pre.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Copy link
Contributor

🤖 Hello there,

We just published version 6.27.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Copy link
Contributor

🤖 Hello there,

We just published version 6.28.2-pre.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

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

Successfully merging this pull request may close these issues.

Children Outlet route intercepts Layout action
2 participants