I'm trying to render the params in the url as an h2 in in the website. But even when I try console.log useParams is empty.
Here's my Router.js file
const Webpages = () => {
return (
<Router>
<Routes>
<Route exact path="/" element={Home()} />
<Route exact path="/ics" element={Comics()} />
<Route path='/ics/:icId' element={Comic()} /> <--------------
<Route exact path="/portfolio" element={Portfolio()} />
<Route exact path="/blog" element={Blog()} />
<Route exact path="/contact" element={Contact()} />
<Route exact path="/store" element={Store()} />
<Route path="*" element={NotFound()} />
</Routes>
</Router>
);
};
export default Webpages;
Here's my ic ponent
import React from 'react';
import NavBar from '../../ponents/NavBar';
import { useParams } from 'react-router-dom';
function Comic() {
let { icId } = useParams();
console.log(icId);
return (
<div>
<NavBar />
<p>{icId}</p>
</div>
)
}
export default Comic
The page element when I go to a random ic works fine, like localhost:3000/ics/465456 but the
tag is empty and the console log is undefined, it's also undefined if I just try to console log useParams()
I'm trying to render the params in the url as an h2 in in the website. But even when I try console.log useParams is empty.
Here's my Router.js file
const Webpages = () => {
return (
<Router>
<Routes>
<Route exact path="/" element={Home()} />
<Route exact path="/ics" element={Comics()} />
<Route path='/ics/:icId' element={Comic()} /> <--------------
<Route exact path="/portfolio" element={Portfolio()} />
<Route exact path="/blog" element={Blog()} />
<Route exact path="/contact" element={Contact()} />
<Route exact path="/store" element={Store()} />
<Route path="*" element={NotFound()} />
</Routes>
</Router>
);
};
export default Webpages;
Here's my ic ponent
import React from 'react';
import NavBar from '../../ponents/NavBar';
import { useParams } from 'react-router-dom';
function Comic() {
let { icId } = useParams();
console.log(icId);
return (
<div>
<NavBar />
<p>{icId}</p>
</div>
)
}
export default Comic
The page element when I go to a random ic works fine, like localhost:3000/ics/465456 but the
tag is empty and the console log is undefined, it's also undefined if I just try to console log useParams()
Share Improve this question edited Aug 21, 2022 at 5:39 Drew Reese 204k18 gold badges245 silver badges273 bronze badges asked Aug 21, 2022 at 5:24 Tom HuginTom Hugin 831 silver badge8 bronze badges1 Answer
Reset to default 4You are directly invoking the React function. Directly invoking React functions is not how React works.
The Route
ponent's element
prop expects a React.ReactNode
, a.k.a. JSX. Pass the ponents as JSX. The JSX is transpiled down to vanilla Javascript and the React framework handles calling your function within the confines of the React ponent lifecycle.
Example:
const Webpages = () => {
return (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/ics" element={<Comics />} />
<Route path='/ics/:icId' element={<Comic />} />
<Route path="/portfolio" element={<Portfolio />} />
<Route path="/blog" element={<Blog />} />
<Route path="/contact" element={<Contact />} />
<Route path="/store" element={<Store />} />
<Route path="*" element={<NotFound />} />
</Routes>
</Router>
);
};
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744396480a4572166.html
评论列表(0条)