简体   繁体   中英

React - how to change properties of a component in another component

function DatTable() {
  return (
      <DataTable
          theme="dark"
          columns={columns}
          selectableRows
          data={FakeData}
      />
  );
};

I've called this component from another file in a dashboard

</Box>
  <DatTable></DatTable>
</Box>

Everything works properly if I change the properties in the original function. What I'm trying to achieve is set a useTheme hook for the component and for that I want to edit the theme inside of the dashboard like so:

</Box>
  <DatTable 
    theme="dark"
  ></DatTable>
</Box>

I've tried changing the properties inside of the dashboard component but it has no effects. I'm also unsure how to turn regular components into React components since the webpage goes white and breaks if I try it that way.

// default data
let columns={};
let data={}

function DatTable(theme="dark", columns=columns, selectableRows='somedefaultdata',data=FakeData) {
  return (
      <DataTable
          theme=theme
          columns={columns}
          selectableRows
          data={FakeData}
      />
  );
};

Now

<DatTable 
    theme="white"
/>

If you this now:

  1. you don't need to send props at all.
  2. If you send props those props will overwrite those default values

Since you pass a theme prop to <DatTable> you need to extract it and then use it as a prop again in <DataTable>

function DatTable({theme}) {
  return (
      <DataTable
          theme={theme} 
          columns={columns}
          selectableRows
          data={FakeData}
      />
  );
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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