简体   繁体   English

React 路由,在 CoreUI 上使用具有不同道具/参数的相同组件

[英]React route, use the same component with different props/parameters on CoreUI

Let's say I have a React component called Component1 , where it makes plots based on the data provided --假设我有一个名为Component1的 React Component1 ,它根据提供的数据绘制图表——

import data_A from "./some/folder/A";
import data_B from "./some/folder/B";
.
.
.
import data_Z from "./some/folder/Z";

const Component1 = (props) => {
    var data = /* decide which data to load using props, will come from a nav link click event */
    return(/* render component with data */);
}

I have another file where I specify all the routes to the components:我有另一个文件,我在其中指定了组件的所有路由:

const Component1 = React.lazy(() => import("./views/components/Component1"));

routes = [
    { path: "/components", name: "Component1", component: Component1, }
    // there are more components but they are not relevant here.
];

and I build the <Switch/> like this:我像这样构建<Switch/>

<Switch>
    {routes.map((route, idx) => {
        return(route.component && (
            <Route key={idx} path={route.path} exact={route.exact} name={route.name}
                render={(props) => (<route.component {...props} />)}
            />
        ));
    })}
    <Redirect from="/" to="/components" />
</Switch>

On my app, I have a navigation bar, which is built on the fly from a list of object specified in a variable.在我的应用程序中,我有一个导航栏,它是根据变量中指定的对象列表动态构建的。 Here each nav-item specifies which data we want to access --这里每个导航项指定了我们要访问的数据——

var navDataList = [
    { _tag: "CSidebarNavItem", name: "Data A", to: "/components" },
    { _tag: "CSidebarNavItem", name: "Data B", to: "/components" },
    .
    .
    .
    { _tag: "CSidebarNavItem", name: "Data Z", to: "/components" },
]

and they are created with this --它们是用这个创建的——

<CSidebarNav>
    <CCreateElement items={navDataList} components={{CSidebarNavItem}} />
</CSidebarNav>

So what I'm basically trying to do is to process data_A , data_B and data_C etc. using the same component Component1 when a user clicks the nav link Data A , Data B and Data C , respectively.所以我基本上要做的是在用户分别单击导航链接Data AData BData C时使用相同的组件Component1处理data_Adata_Bdata_C等。

What I understood is that I have specify which data object I want to process inside the navList as a props.我的理解是,我已经在navList指定了要处理的数据对象作为道具。 But I couldn't figure out how to that.但我无法弄清楚如何做到这一点。

How do I solve this problem?我该如何解决这个问题?

The documentations on CoreUI React components are here: CSidebarNavItem , CSidebarNav , CCreateElement . CoreUI React组件的文档在这里: CSidebarNavItemCSidebarNavCCreateElement


A solution that somewhat works:一个有点奏效的解决方案:

Inside the navDataList , if I specify these parameters, I can access from Component1 :navDataList ,如果我指定这些参数,我可以从Component1访问:

var navDataList = [
    { _tag: "CSidebarNavItem", name: "Data A", 
        to: { 
            pathname: "/components",
            hash: "data_A_will_be_default",
            params: { data: "data_A" },
        },
    }, 
    { _tag: "CSidebarNavItem", name: "Data B", 
        to: { 
            pathname: "/components",
            hash: "data_B",
            params: { data: "data_B" },
        },
    }, 
    ...
];

and then identifying within Component1 :然后在Component1识别:

const Component1 = (props) => {
    var data = data_A; // default data to process
    if (props.location.params && props.location.params.data) {
        if (props.location.params.data === "data_B") {
            data = data_B;
        }
    }
    return(/* render component with data */);
}

But it's not responsive and there is a lag.但它没有响应,并且存在滞后。 The params.data doesn't update instantly. params.data不会立即更新。

I'd define a single "components" route path that takes the data as a route parameter and accessed in the Component1 .我会定义一个“组件”路由路径,它将数据作为路由参数并在Component1访问。

Routes - define a path with route param "/components/:data"路由 - 使用路由参数"/components/:data"定义路径

routes = [
  { path: "/components/:data", name: "Component1", component: Component1, }
  // there are more components but they are not relevant here.
];

Links - link to a specific path with data specified链接 - 链接到指定数据的特定路径

var navDataList = [
  { _tag: "CSidebarNavItem", name: "Data A", to: "/components/A" },
  { _tag: "CSidebarNavItem", name: "Data B", to: "/components/B" },
  .
  .
  .
  { _tag: "CSidebarNavItem", name: "Data Z", to: "/components/Z" },
];

Access match params via Route props or useParams react hook通过Route propsuseParams react hook 访问匹配参数

const Component1 = props => {
  const { data } = props.match.params;
  ...
}

or或者

import { useParams } from 'react-router-dom';

const Component1 = props => {
  const { data } = useParams();
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM