i installed react react-router-dom via NPM i.e
npm install react-router-dom localforage match-sorter sort-by
I am unable to locate the RouterProvider.
i tried to import it in my main file like this
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'
import router from './router.jsx'
import {RouterProvider} from "react-router-dom";
const router = createBrowserRouter([
{
path: "/",
element: (
<div>
<h1>Hello World</h1>
<Link to="about">About Us</Link>
</div>
),
},
{
path: "about",
element: <div>About</div>,
},
]);
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<RouterProvider router={router}/>
{/* <RouterProvider router={router}/>*/}
</React.StrictMode>
)
This is my package.json:
{
"name": "react",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --port=3000",
"build": "vite build",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"@heroicons/react": "^2.0.18",
"@tailwindcss/forms": "^0.5.6",
"localforage": "^1.10.0",
"match-sorter": "^6.3.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^5.3.4",
"sort-by": "^0.0.2"
},
"devDependencies": {
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@vitejs/plugin-react": "^4.0.3",
"autoprefixer": "^10.4.16",
"eslint": "^8.45.0",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.3",
"postcss": "^8.4.30",
"tailwindcss": "^3.3.3",
"vite": "^4.4.5"
}
}
There are no error messages on the page. Has the name RouterProvider been changed?
i found files like BrowserRouter, StaticRouterProvider and also Router. None work though as a router
2
Answers
Don’t you get this error:
router
is already declared in line 5 ?Here’s a simple example to start with:
You are using the incorrect version of
react-router-dom
."react-router-dom": "^5.3.4",
should be"react-router-dom": "^6.4.0",
if you want to use data routers.See Using v6.4 Data APIs.
Try to follow these steps to get the project dependency updated.
cd
to the root project directory.npm uninstall --save react-router-dom
.npm install --save react-router-dom
, verify the version in the package.json file was updated. Current version as of this posting is v6.16.0. You could also verify what is actually installed by runningnpm list react-router-dom
from the root project directory.npm start
or whatever your starting/run script is. It looks like yours would benpm run dev
. I don’t usevite
so I have no idea if you would need to rebuild vianpm run build
first before re-running the app to see the latest changes.If you have issues still, you could try also deleting the
"node_modules"
directory and re-runningnpm install
to install the project dependencies, and restart the app.