-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
History: add getLocationWithParams method #61823
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Size Change: +32 B (0%) Total Size: 1.75 MB
ℹ️ View Unchanged
|
Flaky tests detected in 29291e1. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/9186330670
|
41eeb26
to
96073a0
Compare
const { CreatePatternModal, useAddPatternCategory } = unlock( | ||
editPatternsPrivateApis | ||
); | ||
|
||
export default function AddNewPattern() { | ||
const history = useHistory(); | ||
const { params } = useLocation(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally like the direct hook to retrieve the current route's params. Most routers have a similar hook. React Router has useParams
for instance.
I can reproduce the crash that forced me to avoid using the location object as cache in this PR too.
There's a |
96073a0
to
105f924
Compare
Yes, I can reproduce this, the hook is infinity-loopy. That's fixable.
What are the other problems? What's different with the weakmap is that each That can be fixed either by fixing the effect hook that calls |
No other problems, it's just that using the map trick instead of weak-map fixed the crash above (while keeping the hook...) |
If that's the solution we want to go with, that's totally good for me, I'm not opinionated on how we do the caching, mainly want to avoid the crash. |
Pushed a fix in 29291e1. The PR should be in a mergeable condition now.
In a callback, however, retrieving the params iteratively is technically more correct. It avoids an unneeded rerender. And more importantly, see how in my PR I was able to eliminate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I like this imperative approach with getLocationWithParams
, couldn't spot any additional renders, and it tests well for me.
@jsnajdr can we apply a fix at the framework level instead of the application level, as I assume it might be easy to create this issue again inadvertently. |
@youknowriad One framework-level fix that comes to mind would be history.push( ( params ) => ( {
...params,
postId
} ); That's elegant and you don't need the extra Many Site Editor navigations are of the kind where you want to preserve all query params and only add or remove some of them. This is probably not very typical in mainstream router applications. For example, in Calypso I never encountered a problem of this kind. |
* History: add getLocationWithParams method * Fix effect loop in pages dataview
* History: add getLocationWithParams method * Fix effect loop in pages dataview
Add a new method to
history
calledgetLocationWithParams
. The method is not really new, I'm just moving the existing function of the same name from theRouterProvider
to thehistory
object.The
RouterProvider
can now just use a very straightforwarduseSyncExternalStore
without any complications.The method is memoized using a
WeakMap
so that an identical input (location
) produces an identical output (locationWithParams
). In #61741 (comment) @youknowriad writes about the weakmap implementation:but I cannot confirm this. I put logpoints at the place where
locationWithParams
is recomputed, and didn't notice any extra rerenders that shouldn't be there.I'm also updating many places where the
locationWithParams
value was used in a non-reactive way, in event handlers and callbacks. Here I'm migrating reactiveuseLocation
calls to imperativehistory.getLocationWithParams()
calls. That should save a lot of redundant rerenders. I first noticed this when working on #61230 and now I'm fixing it.This overlaps with #61741 -- depending on which one is merged first, there will be conflicts. But I believe they will be easy to resolve.