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

Compatibility with react-use useSearchParam #164

Closed
wants to merge 2 commits into from
Closed

Compatibility with react-use useSearchParam #164

wants to merge 2 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Jan 2, 2021

I commented about this on #58 , so I thought I should open a PR to fix the problem I ran into. Currently, wouter doesn't have support for search params so I tried to use the useSearchParam hook from react-use, problem is that wouter and react-use added listeners for different events, making them incompatible since useSearchParam doesn't update when wouter does.

Plus, this change also follows the naming convention of window events.

Copy link
Author

@ghost ghost left a comment

Choose a reason for hiding this comment

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

🤦‍♂️ The event strings are not just used to add and remove event listeners, but also to call the events. So this means we would call replacestate instead of replaceState on the history API...

use-location.js Outdated Show resolved Hide resolved
@ghost
Copy link
Author

ghost commented Jan 7, 2021

@molefrog I discarted the commit that changed the event names declarations and made it so that they are transformed to lowercase when registering listeners. Do you think this is ok? This way replaceState is registered as replacestate.

@@ -6,7 +6,7 @@ import { useEffect, useRef, useState, useCallback } from "./react-deps.js";
const eventPopstate = "popstate";
const eventPushState = "pushState";
const eventReplaceState = "replaceState";
export const events = [eventPopstate, eventPushState, eventReplaceState];
Copy link
Author

Choose a reason for hiding this comment

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

I also remove this export statement because it isn't being used by any other file

Copy link
Owner

Choose a reason for hiding this comment

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

Well, this is intended, although not documented. Some apps use the name of the events in order to subscribe to their own location changes. I'm thinking about removing it in the next major release though.

Copy link
Author

Choose a reason for hiding this comment

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

Ah ok. Thanks for clarifying.

@ghost
Copy link
Author

ghost commented Jan 10, 2021

@molefrog
Copy link
Owner

Hey @joaopaulobdac, I don't think the names of these events really matter. The events that wouter emits are virtual and non-standard: there is no built-in way to subscribe to location changes, so wouter matches native methods and triggers custom events. If both wouter and react-use patch the History API, they should play along well.

Take a look at #169, I have a feeling that the reason wouter didn't react to search changes was that it applied caching that ignore the search query.

@ghost
Copy link
Author

ghost commented Jan 26, 2021

If both wouter and react-use patch the History API, they should play along well.

I think that one assignment overrides another. If I do history[method] = 'a' and then history[method] = 'b', the first assignment would be gone, no?

@molefrog
Copy link
Owner

If both wouter and react-use patch the History API, they should play along well.

I think that one assignment overrides another. If I do history[method] = 'a' and then history[method] = 'b', the first assignment would be gone, no?

Not really, the original method (prior to patching) is always called with the same arguments before an event is triggered.
react-use does it here: https://github.com/streamich/react-use/blob/206f4b037109bcb8325a156edfe3ce6795b7976e/src/useLocation.ts#L9

and wouter does it here: https://github.com/molefrog/wouter/blob/master/use-location.js#L64

In the end, it creates a chain of hooks, so both wouter and react-use events should be fired. The worst thing that could happen is that they could have identical names for the events, but that's fine: the app will be rendered twice which isn't critical in case of a React app.

@molefrog
Copy link
Owner

There are some weak points that patching has, well described in this issue: #167

However, these are mostly performance-related concerns.

@ghost
Copy link
Author

ghost commented Jan 26, 2021

Great, I'll close this since it seems that with #169 we can react to changes to search params with useLocation

@ghost ghost closed this Jan 26, 2021
@molefrog
Copy link
Owner

molefrog commented Jan 26, 2021

@joaopaulobdac There is a new version that I've just published v2.7.2, hope it helps to resolve the problem.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant