2023 Answer - React Typescript error: Property 'history' does not exist on type 'IntrinsicAttributes'
The exact typescript error I got was :
Type '{ children: Element; history: BrowserHistory; }' is not assignable to type 'IntrinsicAttributes & BrowserRouterProps'.
Property 'history' does not exist on type 'IntrinsicAttributes & BrowserRouterProps'.
After hours of many research and troubleshooting the solution that resolves the issue is stated below.
Step 1 - install "history" into you project dependency
npm i history
Step 2 - import Router and createMemoryHistory from the correct sources
Importing BrowserRouter from "react-router-dom" is outdated, and also is importing {createBrowserHistory} from "history", so we need to import Router and createMemoryHistory as seen below...
import { BrowserRouter } from "react-router-dom";
import { createMemoryHistory } from "history";
Step 3 - define history from createMemoryHistory
const history = createMemoryHistory();
Step 4 - pass the history props to your Router
Finally you pass history={history} and navigator={history}, as seen below...
<Router history={history} navigator={history}>
<React.StrictMode>
<App />
</React.StrictMode>
</Router>
Finally your entire "index.tsx" should look like the following...
import React from "react";
import ReactDOM from "react-dom/client";
import { BrowserRouter } from "react-router-dom";
import { createMemoryHistory } from "history";
import "./index.css";
import App from "./App";
import * as serviceWorkerRegistration from "./serviceWorkerRegistration";
import reportWebVitals from "./reportWebVitals";
const history = createMemoryHistory();
const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);
root.render(
<BrowserRouter history={history} navigator={history}>
<React.StrictMode>
<App />
</React.StrictMode>
</BrowserRouter>
);
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers:
serviceWorkerRegistration.register();
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more:
reportWebVitals();
history
object in v6.x. You should also instead now use one of the higher level routers, i.e.BrowserRouter
, etc. – Napoleon