-
Notifications
You must be signed in to change notification settings - Fork 96
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
ReactRouterRoute breaks with React Router v6 #108
Comments
Any update on this? @pbeshai are you planning on supporting React Router v6? It will probably be out soon. |
If anyone has ideas on how to support it, please feel free to create a PR or share your idea here. I haven't even looked at RR 6 yet. I'd be ok with adding a different prop to the QueryParamProvider for RR6 specifically if that's required. Otherwise, I'll probably look at it if I ever decide to switch to RR 6. |
For what it's worth, react-router v6 now includes useSearchParams. It's lower level and not as convenient as this library but could serve as a building block toward adding RR6 compatibility. |
Wowwww it took so many years for this to happen, finally my dreams of them just providing it are coming close to being realized 😭 😁 Thank you for sharing! Perhaps it will be sufficient to combine their useSearchParams hook with the encoders from https://github.com/pbeshai/serialize-query-params to get a similar experience in the meantime. |
so how we can use this with v6 ? |
I don't know if the import { createBrowserHistory } from 'history'
const history = createBrowserHistory()
function App() {
return (
<QueryParamProvider history={history} location={history.location}>
<ActualApp />
</QueryParamProvider>
)
}
ReactDOM.render(
<HistoryRouter history={history}>
<App />
</HistoryRouter>,
document.getElementById('root')
); I tried adding this to the examples folder, but I ran into lots of issues trying to get the examples running. |
Hi all, finally took some time to see what was needed to get this working. @noah-potter's suggestion might work too, I didn't investigate, but an alternative style is available in the examples directory. https://github.com/pbeshai/use-query-params/tree/master/examples/react-router-6 Update See a TypeScript version here Perhaps after release I'll look towards building it into the library, but for now it should give you an idea. The basics repeated here: import * as React from 'react';
import {
BrowserRouter,
Routes,
Route,
useNavigate,
useLocation,
} from 'react-router-dom';
import {
NumberParam,
QueryParamProvider,
useQueryParam,
} from 'use-query-params';
const App = ({}) => {
return (
<BrowserRouter>
{/* adapt for react-router v6 */}
<QueryParamProvider ReactRouterRoute={RouteAdapter}>
<Routes>
<Route path="/" element={<Home />} />
</Routes>
</QueryParamProvider>
</BrowserRouter>
);
};
/**
* This is the main thing you need to use to adapt the react-router v6
* API to what use-query-params expects.
*
* Pass this as the `ReactRouterRoute` prop to QueryParamProvider.
*/
const RouteAdapter = ({ children }) => {
const navigate = useNavigate();
const location = useLocation();
const adaptedHistory = React.useMemo(
() => ({
replace(location) {
navigate(location, { replace: true, state: location.state });
},
push(location) {
navigate(location, { replace: false, state: location.state });
},
}),
[navigate]
);
return children({ history: adaptedHistory, location });
}; |
v6 has been released now, any chance of integrating the above into the this library? |
If anyone else have come across this issue I made a custom hook that works out of the box with react-router v6. Could probably make it into it's own lib if there is an interest. I haven't had the time to test it that much but it seems to be working fine.
This will work but be aware that i'm using an unsafe context from react-router There is actually a PR and an issue on react-router where they propose to fix that so that the |
@pbeshai does this solution not re-render the provider and all its children every time location changes? That is what it was doing for me. |
I ended up writing an adapter that transformed |
@kuberlog I have tried just about everything to get |
This same example does not work in the test config file with React router v6, not able to set query parameter with useQueryParams from unit testcases. Could you please suggest? |
Hi, any updates here to make use-query-params compatible with react-router v6? |
I ran into some weird bug using this workaround solution with Navigating to a different location causes the inside of the adapted history to be replaced with the stale path and prevents navigation. On the second try, navigation works fine. It seems that navigation inside const RouteV6Adapter: FC<{ children?: ReactNode }> = ({ children }) => {
const navigate = useNavigate();
const location = useLocation();
const adaptedHistory = useMemo(
() => ({
push: ({ search, state }: Location) => navigate({ search }, { state }),
replace: ({ search, state }: Location) => navigate({ search }, { replace: true, state }),
}),
[navigate],
);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return children({ history: adaptedHistory, location });
}; @pbeshai what do u think? Maybe better to suggest this solution in examples |
how do you use this in a React App? |
Here is an Example App code.
|
add type definition:
|
Hi there, please give the React Router 6 adapter in v2.0.0-rc.0 a try and let me know if you run into issues. See the changelog for details. |
This is how I've typed import type { Location } from "react-router-dom";
...
replace(location: Location) { |
This library no longer works with React Router v6 (currently in alpha).
React Router v6 no longer allows for functions as child components. The following code from this library breaks:
use-query-params/src/QueryParamProvider.tsx
Lines 183 to 194 in 32d79f2
The new signature of
Route
in React Router v6 is:https://github.com/ReactTraining/react-router/blob/dev/packages/react-router/index.tsx#L235-L240
The text was updated successfully, but these errors were encountered: