React TypeError Cannot read properties of undefined (reading ‘params’)
Hello guys, in this article, we will explain to you how to solve typeerror: Cannot Read Properties Of Undefined (Reading ‘Params’) in react. we will give you a simple example of how to solve Cannot Read Properties Of Undefined (Reading ‘Params’) in react.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import { Container } from 'react-bootstrap'; import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'; import Product from './components/Product' function App() { return ( <Router> <Header /> <main className="py-3"> <Container> <Routes> <Route path='/product/:id' element={<Product/>} /> </Routes> </Container> </main> <Footer /> </Router> ); } export default App; |
In this above example script code, we pass the product id to child component. but some times it not working and it return the error TypeError: Cannot Read Properties Of Undefined (Reading ‘Params’) can be resolved.
In this below example, we fix that error using the params as useParams(). so you can see below example.
1 2 3 4 5 6 7 8 9 10 11 12 13 | import { Link, useParams } from 'react-router-dom'; function Product() { const { id } = useParams(); console.log(id); //output product id return ( <div> <p>here show product id get using params</p> </div> ); } export default Product; |
Please follow and like us: