如何通过导出在非索引文件中呈现反应数据网格?

How to render react-data-grid in non index file with exporting?

我正在使用 react-data-grid 来渲染数据,但它只在 index.js 文件中工作,但是当我在普通文件中使用时,我无法通过定义 class & 还有函数名。

尝试使用 class 名称和函数名称导出。

import React, { useState, Component } from "react";
import ReactDOM from "react-dom";
import ReactDataGrid from "react-data-grid";
import { Toolbar, Data } from "react-data-grid-addons";
import createRowData from "./createRowData";

const defaultColumnProperties = {
  filterable: true,
  width: 200
};

const selectors = Data.Selectors;

const columns = [
  {
    key: "ID",
    name: "ID"
  },
  {
    key: "Data",
    name: "Data"
  },
  {
    key: "firstname",
    name: "first name"
  },
  {
    key: "amount",
    name: "Amount"
  }
].map(c => ({ ...c, ...defaultColumnProperties }));

const ROW_COUNT = 50;

const handleFilterChange = filter => filters => {
  const newFilters = { ...filters };
  if (filter.filterTerm) {
    newFilters[filter.column.key] = filter;
  } else {
    delete newFilters[filter.column.key];
  }
  return newFilters;
};

function getRows(rows, filters) {
  return selectors.getRows({ rows, filters });
}

function Example({ rows }) {
  const [filters, setFilters] = useState({});
  const filteredRows = getRows(rows, filters);
  return (
    <ReactDataGrid
      columns={columns}
      rowGetter={i => filteredRows[i]}
      rowsCount={filteredRows.length}
      minHeight={300}
      minWidth={1300}
      toolbar={<Toolbar enableFilter={true} />}
      onAddFilter={filter => setFilters(handleFilterChange(filter))}
      onClearFilters={() => setFilters({})}
    />
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<Example rows={createRowData(50)} />, rootElement);

需要导出文件以进行路由,需要从另一个文件获取数据以在网格视图中发布以及搜索字段的筛选功能。

我想我理解你面临的问题。你能把上面的改成这个吗

 const ROW_COUNT = 50;


const handleFilterChange = filter => filters => {
    const newFilters = { ...filters };
    if (filter.filterTerm) {
      newFilters[filter.column.key] = filter;
    } else {
      delete newFilters[filter.column.key];
    }
    return newFilters;
  };

  function getRows(rows, filters) {
    return selectors.getRows({ rows, filters });
  }

function Example({rows}) {
    //const rows = createRowData(50);
    const [filters, setFilters] = useState({});
    const filteredRows = getRows(rows, filters);
    return (
      <ReactDataGrid
        columns={columns}
        rowGetter={i => filteredRows[i]}
        rowsCount={filteredRows.length}
        minHeight={500}
        toolbar={<Toolbar enableFilter={true} />}
        onAddFilter={filter => setFilters(handleFilterChange(filter))}
        onClearFilters={() => setFilters({})}
      />
    );
  }
class datagrid extends Component {

    render() {
        return(
        <Example rows={createRowData(50)} />);
    }
}

export default datagrid;