首页 > 解决方案 > 来自路线的参数

问题描述

我希望从 JSON 映射路径值。路径值将具有需要从 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) => { 

标签: javascriptreactjsreact-nativeroutesreact-router

解决方案


<Router />组件只需要一个子组件。您可以在此处的文档中看到它

export default function Nav() {
  return (
    <Router>
      <Switch>
        <Route
          render={({ location }) => {
            const { pathname } = location;
            return <div>{pathname}</div>;
          }}
        />
      </Switch>
    </Router>
  );
}

然后,您可以将组件包含在<nav />组件之外<Router />,也可以包含一个<div />内部,同时包含两者。


推荐阅读