-
-
Notifications
You must be signed in to change notification settings - Fork 156
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
Potential useSearch
bug not triggering updates when used with urql
#393
Comments
After playing around and some debugging, I notice that when I do
export const useSearch = ({ ssrSearch = "" } = {}) => {
return useLocationProperty(
() => location.search,
() => ssrSearch
);
}; Instead of const currentSearch = () => location.search;
export const useSearch = ({ ssrSearch = "" } = {}) =>
useLocationProperty(currentSearch, () => ssrSearch); The issue seems to be fixed. Could this be a potential bug with how the library uses Screen.Recording.2023-12-02.at.4.38.19.PM.movIf this is really the cause of the bug, does that mean that export const useSearch = ({ ssrSearch = "" } = {}) =>
useLocationProperty(
() => location.search,
() => ssrSearch
);
// ...
export const usePathname = ({ ssrPath } = {}) =>
useLocationProperty(
() => location.pathname,
ssrPath ? () => ssrPath : location.pathname
);
// ...
export const useHistoryState = () =>
useLocationProperty(
() => history.state,
() => null
); I am guessing, when we define const currentSearch = () => location.search; The value of Any thoughts / comments are welcomed! |
@junwen-k Hi! My guess it that const useSearchParams = () => {
const [,navigate] = useLocation()
const search = useSearch()
const params = new URLSearchParams(window.location.search)
const setSearchParams = (obj) => {
navigate(`?tab=${obj.tab}`)
}
return [params, setSearchParams]
} |
Also, take a look at #391 |
Hi, thank you for replying. I've tested with the above snippet and the issue seems to persist. 👀
import { useLocation, useSearch } from "wouter";
export const useSearchParams = () => {
const [, navigate] = useLocation();
const search = useSearch();
const params = new URLSearchParams(window.location.search);
const setSearchParams = (obj) => {
navigate(`?tab=${obj.tab}`);
};
return [params, setSearchParams];
}; Screen.Recording.2023-12-12.at.4.03.05.PM.movYou can see that for a split second, Tab 3 works for a while, then it won't work again 🤷🏻♂️ (0:20: Refresh with |
Right, I was able to reproduce it locally. What bothers me is that React Router also doesn't re-render when the query string changes from the outside. Try running: |
I notice that when I do const subscribeToLocationUpdates = (callback) => {
for (const event of events) {
addEventListener(event, (...props) => {
console.log("event", event, ...props);
callback(props);
});
}
return () => {
for (const event of events) {
removeEventListener(event, callback);
}
};
}; Console actually output:
Which means export const useSearch = ({ ssrSearch = "" } = {}) =>
useLocationProperty(
() => location.search, // Inlined
() => ssrSearch
); May I know if this is not the right approach? I have the impression that somehow the value of I've also noticed that
Not sure if running |
I did some digging: React Router uses its own format of
Interestingly, 2) works well, while 1) is still broken. It proves that the issue is indeed in wouter, but it also tells us that it's not just the search string hook. I'll keep looking |
Hey @junwen-k any update on this? I'm curious if you found out the issue. |
Hi, thank you for following up. As mentioned earlier, I've actually found out a solution that worked on my end. The changes that fixes the issue was: - const currentSearch = () => location.search;
-
export const useSearch = ({ ssrSearch = "" } = {}) =>
- useLocationProperty(currentSearch, () => ssrSearch);
+ useLocationProperty(
+ () => location.search,
+ () => ssrSearch
+ ); I am also wondering if we should change - const currentHistoryState = () => history.state;
-
export const useHistoryState = () =>
- useLocationProperty(currentHistoryState, () => null);
+ useLocationProperty(
+ () => history.state,
+ () => null
+ ); However as mentioned earlier, I am not sure if this is the right approach to the issue.
Currently as a workaround, I am using my upstream branch which you can take a look here. Note that this is tightly related to #391 as the |
Suffering from the same bug. Would appreciate if the connected PR was merged soon. |
Greetings, I've been scratching my head over this issue for quite awhile.
Following #368, I've been using the
useSearchParams
hook and it has been working fine, until I started integrating withurql
'suseQuery
.Issue
useSearch
does not rerender if it begins with query params and revisited. For example, if I land on the page with URLhttp://localhost:5173/?tab=1
I cannot go back to original tab after switching to another tab. (URL has been updated to
?tab=1
, but React does not rerender the components. You may see the video below:Screen.Recording.2023-12-01.at.8.59.27.PM.mov
Reproduction Steps
Clone https://github.com/junwen-k/react-router-wouter
Install dependencies
Start Server
Experiment with the page, with default
?tab=1
triggers the issueContext
Suspect
pushState
event does not trigger, hence React does not rerenders the component.React Router's version works perfectly, so I suspect this is something to do with
wouter
.The text was updated successfully, but these errors were encountered: