Home > Software design >  Change an array loaded from another component in React.js jsx
Change an array loaded from another component in React.js jsx

Time:01-14

I am trying to change a hardcoded array within another JSX file. the first file routes.js. I tried loading the array then changing it . it just changes the loaded data not the array directly from the other file. How do i write to the other JSX array from the main component.

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;

code used in main jsx file. I want to be able to write to the remote array changing its values.

const handleSubmit = (event) => {
  event.preventDefault();

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

CodePudding user response:

You can't change the array itself because it's a const. You could change it to a let and then export it like this:

EDIT

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,
  },
];

Then to use it in another jsx component you can import it like this.

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

  •  Tags:  
  • Related