首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改从React.js jsx中的另一个组件加载的数组

更改从React.js jsx中的另一个组件加载的数组
EN

Stack Overflow用户
提问于 2022-01-13 18:15:14
回答 1查看 191关注 0票数 2

我正在试图更改另一个JSX文件中的硬编码数组。第一个文件routes.js。我试着加载数组,然后更改它。它只是更改加载的数据,而不是直接从另一个文件更改数组。如何从主组件写入另一个JSX数组。

代码语言:javascript
复制
const routes = [
  {
    type: "collapse",
    name: "Our Mission",
    key: "dashboards",
    icon: <Shop size="12px" />,
    collapse: [
      {
        name: "Ways We can Help",
        key: "default",
        route: "/dashboards/default",
        component: Default,
      },
      {
        name: "How It Works",
        key: "automotive",
        route: "/dashboards/automotive",
        component: Automotive,
      },
      {
        name: "Who We Are",
        key: "smart-home",
        route: "/dashboards/smart-home",
        component: SmartHome,
      },
    ],
  },
  { type: "title", title: " ", key: "space1" },
  {
    type: "collapse",
    name: "Services",
    key: "services",
    icon: <Shop size="12px" />,
    href: "https://github.com/creativetimofficial/ct-soft-ui-dashboard-pro-material-ui/blob/main/CHANGELOG.md",
    component: Default,
    noCollapse: true,
  },
  {
    type: "collapse",
    name: "Products",
    key: "products",
    icon: <Shop size="12px" />,
    href: "https://github.com/creativetimofficial/ct-soft-ui-dashboard-pro-material-ui/blob/main/CHANGELOG.md",
    component: Default,
    noCollapse: true,
  },
];

export default routes;

在主jsx文件中使用的代码。我希望能够写入远程数组,更改其值。

代码语言:javascript
复制
const handleSubmit = (event) => {
  event.preventDefault();

  // I want to push or filter with the code below
  {
    routes.length = 0;
   
    routes.map((route) => console.log({ route }));
  }
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-13 18:25:51

您不能更改数组本身,因为它是一个const。您可以将其更改为let,然后按如下方式导出:

编辑

代码语言:javascript
复制
export let routes = [
  {
    type: "collapse",
    name: "Our Mission",
    key: "dashboards",
    icon: <Shop size="12px" />,
    collapse: [
      {
        name: "Ways We can Help",
        key: "default",
        route: "/dashboards/default",
        component: Default,
      },
      {
        name: "How It Works",
        key: "automotive",
        route: "/dashboards/automotive",
        component: Automotive,
      },
      {
        name: "Who We Are",
        key: "smart-home",
        route: "/dashboards/smart-home",
        component: SmartHome,
      },
    ],
  },
  { type: "title", title: " ", key: "space1" },
  {
    type: "collapse",
    name: "Services",
    key: "services",
    icon: <Shop size="12px" />,
    href: "https://github.com/creativetimofficial/ct-soft-ui-dashboard-pro-material-ui/blob/main/CHANGELOG.md",
    component: Default,
    noCollapse: true,
  },
  {
    type: "collapse",
    name: "Products",
    key: "products",
    icon: <Shop size="12px" />,
    href: "https://github.com/creativetimofficial/ct-soft-ui-dashboard-pro-material-ui/blob/main/CHANGELOG.md",
    component: Default,
    noCollapse: true,
  },
];

然后,要在另一个jsx组件中使用它,可以像这样导入它。

import {routes} from '../yourPathToIt/main'

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70701220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档