英文:
Params from Route
问题
我想要从JSON中映射pathvalue。 pathvalue将包含需要从JSON加载的页面名称。
<Switch>
<Route
render={({ location }) => {
const { pathname } = location;
const pathvalue = { pathname }
return (
<>
<div>
</div>
<div className="container">
{
data.pathvalue.map((rows, i) => { //而不是告诉页面名称,我将页面名称存储在pathvalue const中。这是否可能?
return (
<div key={i} className="row">
{rows.row.map((component, i) => {
请注意,代码中的注释(//而不是告诉页面名称,我将页面名称存储在pathvalue const中。这是否可能?)已经保留在翻译中。
英文:
I want the pathvalue to be mapped from JSON. The pathvalue will have the page name which needs to load from JSON.
<Switch>
<Route
render={({ location }) => {
const {pathname} = location;
const pathvalue = {pathname}
return (
<>
<div>
</div>
<div className="container">
{
data.pathvalue.map((rows, i) => { //Instead of telling the page name, I stored the page name in pathvalue const. Is it possible?
return (
<div key={i} className="row">
{rows.row.map((component, i) => {
答案1
得分: 0
<Router />
组件只接受一个子组件。您可以在这里查看文档
export default function Nav() {
return (
<Router>
<Switch>
<Route
render={({ location }) => {
const { pathname } = location;
return <div>{pathname}</div>;
}}
/>
</Switch>
</Router>
);
}
然后,您可以在 <Router />
组件之外包含 <nav />
组件,或者在内部包含一个包含两者的单个 <div />
。
英文:
The <Router />
component only takes a single child component. You can see it in the documentation here
export default function Nav() {
return (
<Router>
<Switch>
<Route
render={({ location }) => {
const { pathname } = location;
return <div>{pathname}</div>;
}}
/>
</Switch>
</Router>
);
}
You can then either include the <nav />
component outside the <Router />
component, or include a single <div />
inside with both included.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论