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 fetcher data layer #10961

Merged
merged 3 commits into from Oct 26, 2023
Merged

Add fetcher data layer #10961

merged 3 commits into from Oct 26, 2023

Conversation

brophdawg11
Copy link
Contributor

@brophdawg11 brophdawg11 commented Oct 25, 2023

Dependent on #10960

Adds a fetcher data layer to RouterProvider to setup the 3rd (and final) PR for this work (a future flag to change the fetcher cleanup/persistence behavior)

@changeset-bot
Copy link

changeset-bot bot commented Oct 25, 2023

🦋 Changeset detected

Latest commit: a9c1886

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

This PR includes changesets to release 4 packages
Name Type
react-router-dom Patch
react-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

Comment on lines +353 to +357
type FetchersContextObject = {
fetcherData: Map<string, any>;
register: (key: string) => void;
unregister: (key: string) => void;
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fetcher data lives in the React layer now, and we use ref counting via register/unregister to know when to remove from fetcherData

@@ -457,6 +472,12 @@ export function RouterProvider({
newState: RouterState,
{ unstable_viewTransitionOpts: viewTransitionOpts }
) => {
newState.fetchers.forEach((fetcher, key) => {
if (fetcher.data !== undefined) {
fetcherData.current.set(key, fetcher.data);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fetcher data hand-off from the router to the React data layer

@@ -1216,6 +1241,49 @@ function useDataRouterState(hookName: DataRouterStateHook) {
return state;
}

function useFetcherDataLayer() {
let fetcherRefs = React.useRef<Map<string, number>>(new Map());
let fetcherData = React.useRef<Map<string, any>>(new Map());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we're OK using a ref here but I need to bang on some edge cases to be sure - all tests and such pass as-is which is reassuring. The idea is that anytime a fetcher.data needs to update - a fetcher will have changed state which will be what triggers the re-render, and we don't need useState version of fetcherData.

@brophdawg11 brophdawg11 merged commit cb2d911 into dev Oct 26, 2023
3 checks passed
@brophdawg11 brophdawg11 deleted the brophdawg11/2-fetcher-data branch October 26, 2023 19:17
@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 6.18.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.18.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.

None yet

1 participant