Replies: 3 comments 2 replies
-
Ok I found a way to solve this, I made some adjustments to the WindowHistoryAdapter. Now when the query params are being changed, every component which uses for example I still don't know if there is a proper way, might be that this solution is not necessary/good at all. import { useEffect, useState } from "react";
import { PartialLocation, QueryParamAdapterComponent } from "use-query-params";
function makeAdapter({ onChange }: { onChange: () => void }) {
const adapter = {
replace(location: PartialLocation) {
window.history.replaceState(location.state, "", location.search || "?");
onChange();
},
push(location: PartialLocation) {
window.history.pushState(location.state, "", location.search || "?");
onChange();
},
get location() {
return window.location;
},
};
return adapter;
}
/**
* Adapts standard DOM window history to work with our
* { replace, push } interface.
*/
export const RerenderingWindowHistoryAdapter: QueryParamAdapterComponent = ({
children,
}) => {
const refreshAdapter = () =>
setAdapter(makeAdapter({ onChange: refreshAdapter }));
const [adapter, setAdapter] = useState(
makeAdapter({ onChange: refreshAdapter })
);
useEffect(() => {
const handlePopState = () => {
refreshAdapter();
};
window.addEventListener("popstate", handlePopState);
return () => {
window.removeEventListener("popstate", handlePopState);
};
}, []);
return children(adapter);
}; |
Beta Was this translation helpful? Give feedback.
-
Hi @websaid. Thank you for your efforts. Thanks again mate! |
Beta Was this translation helpful? Give feedback.
-
Hi @websaid The project source code is rather convoluted, I don't understand why things are implemented the way they are, so my solution may not be the best. You can expand on it if you want. Just keep me posted. You can find the code here: |
Beta Was this translation helpful? Give feedback.
-
Im trying to keep it short, will go more into detail if the awnser is no:
Im using in a react app (without React-Router) the WindowHistoryAdapter and my components dont rerender if the matching query param changes. Is this supposed to be like that?
Thanks for any assistance!
Beta Was this translation helpful? Give feedback.
All reactions