question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReactRouterRoute breaks with React Router v6

See original GitHub issue

This library no longer works with React Router v6 (currently in alpha).

Warning: Failed prop type: Invalid prop `children` supplied to `Route`, expected a ReactNode.
    in Route (created by QueryParamProvider)

React Router v6 no longer allows for functions as child components. The following code from this library breaks:

https://github.com/pbeshai/use-query-params/blob/32d79f2a47257723fa8459047b2eab1898a61c59/src/QueryParamProvider.tsx#L183-L194

The new signature of Route in React Router v6 is:

export interface RouteProps {
  caseSensitive?: boolean;
  children?: React.ReactNode;
  element?: React.ReactElement | null;
  path?: string;
}

https://github.com/ReactTraining/react-router/blob/dev/packages/react-router/index.tsx#L235-L240

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:16
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

26reactions
pbeshaicommented, Dec 3, 2021

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 });
};
11reactions
SimonSchickcommented, Nov 4, 2021

v6 has been released now, any chance of integrating the above into the this library?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrating to React Router v6: A complete guide
Migrate your React Router applications from v5 to v6 with this in-depth guide, including a review of additions and improvements from v5.
Read more >
Upgrading from v5 v6.6.1 - React Router
It also introduces a few breaking changes from version 5. ... This is a React Router v6 app import { BrowserRouter, Routes, Route,...
Read more >
Ultimate React Router v6 Guide
This article will be broken down into 4 sections. React Router Basics; Advanced Route Definitions; Handling Navigation; Routers In Depth ...
Read more >
Routing is not working using react-router-dom-v6.4.2 [duplicate]
I had created a 3 components in /src/pages named AddCoupon , Addvertical , Addmetadata . Now then I created a file Routes.js in...
Read more >
Introduction to react-router-dom: setting up your first routes
React JS as a Single Page App library doesn't include built-in routing capabilities as Angular does; it needs a library called ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found