wmr
wmr copied to clipboard
(preact-iso): Use pushState/ replaceState for useLocation().route method
At this moment when using useLocation().route('/foobar') window location doesn't change: neither history.pushState nor history.replaceState method is used.
This pull request
- changes method behavior in that window location will be added to history via
pushState. - allows using object as a parameter so it's possible to use
replaceState
// Use history.pushState
route('/foobar);
route({ url: '/foobar' });
route({ url: '/foobar', replace: false });
// Use history.replaceState
route({ url: '/foobar', replace: true });
Originally the useReducer hook dispatcher had a third push argument, but dispatchers consume only two arguments.
https://github.com/preactjs/wmr/blob/58f1bffd108f45c1ac5759f744f484b5d6a8fcca/packages/preact-iso/router.js#L4
I'm not sure if I should bump patch or minor version with changeset?
🦋 Changeset detected
Latest commit: 3d0d9f47d192fce8f2ce954520f0e60f2318a10c
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
| Name | Type |
|---|---|
| preact-iso | 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
@developit
Unfortunately passing replace flag as additional argument doesn't work, as route is actually a reducers dispatcher, which accepts only one argument (see docs > useReducer):
https://github.com/preactjs/wmr/blob/58f1bffd108f45c1ac5759f744f484b5d6a8fcca/packages/preact-iso/router.js#L44-L51
Probably we would have to refactor router code to not use reducer in this case (which may be not a bad idea to handle history outside/ without reducer).
I prefer route(url, replace) as well, but as long as there is no better solution, let's keep this PR opened for reference
I've created new PR in #424 to fix the bug with location change after using useLocation().route(path).
After it's resolved it will be easier to move forward with adding an option such as route(path, replace) either with this PR new one.
Nice - thanks for pulling that out, just merged it.
I've updated PR to use code suggested in https://github.com/preactjs/wmr/pull/413#discussion_r593948478
However I'm not happy with the route({ url, replace }) signature (proposed by me) and would prefer route(url, replace) instead.
Let's not merge this PR yet, we can do better.
@piotr-cz I think we might be able to create a wrapper function in the provider that swaps the argument signature:
- const [url, route] = useReducer(UPDATE, location.pathname + location.search);
+ const [url, doRoute] = useReducer(UPDATE, location.pathname + location.search);
+ const route = useCallback((url, replace) => doRoute({ url, replace }), []);
It might even be possible to simplify the history event handling this way too.
@developit Updated, thank you for suggestion!
Just one question:
Should I add changeset with patch or minor version bump?
As the route method is not documented, I guess patch is enough.
I'd go for patch yea