I'm learning how to use useMatch() from "react-router-dom"
I am getting the error below and my web page is blank:
Uncaught TypeError: Cannot read properties of undefined (reading 'path')
at matchPath (index.tsx:1148)
at index.tsx:481
at mountMemo (react-dom.development.js:15846)
at Object.useMemo (react-dom.development.js:16219)
at useMemo (react.development.js:1532)
at useMatch (index.tsx:481)
at About (About.js:7)
at renderWithHooks (react-dom.development.js:14985)
at mountIndeterminateComponent (react-dom.development.js:17811)
at beginWork (react-dom.development.js:19049)
my code is below
import React from "react"
import { useMatch } from "react-router-dom"
import { Link, Route } from "react-router-dom"
import SinglePage from "./SinglePage"
const About = () => {
const { url, path } = useMatch()
console.log("hello usematch!", useMatch())
return (
<div>
<ul>
<li>
<Link to={`${url}/about-app`}>About App</Link>
</li>
<li>
<Link to={`${url}/about-author`}>About Author</Link>
</li>
</ul>
<Route path={`${path}/:slug`}>
<SinglePage />
</Route>
</div>
)
}
export default About
And below is the index.js file where we get to this about page:
import React from "react";
import ReactDOM from "react-dom";
//component file
import TodoContainer from "./functionBased/components/TodoContainer";
import { Route, Routes, BrowserRouter as Router } from "react-router-dom";
//stylesheet
import "./functionBased/App.css";
import About from "./pages/About";
import NotMatch from "./pages/NotMatch";
//stylesheet
// import "./App.css"
ReactDOM.render(
<React.StrictMode>
<Router>
<>
<Routes>
<Route exact path="/" element={<TodoContainer />} />
<Route path="/about" element={<About />} />
<Route path="*" element={<NotMatch />} />
</Routes>
</>
</Router>
</React.StrictMode>,
document.getElementById("root")
);
I've tried going through the api docs but unclear about why this is not working and how to make the page load. I am trying to return the nearest current route match within the context of where it's called.
I am quite new to react and router apis.
Thanks so much