我正在尝试使用 Electron 创建一个 React.js 应用程序,并且我正在尝试创建一个数据网格来显示我的数据。我最初有 Material-UI Data-grid 组件,但它给了我以下错误:error
我花了大约 3 天的时间试图弄清楚为什么这不起作用,然后我最终决定尝试另一个数据网格包,我安装了 react-data-grid,但得到了相同的结果。
这与我编写代码的方式有关,还是包和 Electron 之间的兼容性问题,我该如何解决?
App.tsx 使用@material-iu/data-grid
import React from 'react'
import ReactDOM from 'react-dom'
// import TestComponent from './test'
import { DataGrid } from '@material-ui/data-grid'
const App: React.FC = function () {
return (
<div style={{ textAlign: 'center' }}>
<p>Hello</p>
</div>
)
}
const columns = [
{ field: 'id', headerName: 'ID', width: 70 },
{ field: 'firstName', headerName: 'First name', width: 130 },
{ field: 'lastName', headerName: 'Last name', width: 130 },
{
field: 'age',
headerName: 'Age',
type: 'number',
width: 90,
},
{
field: 'fullName',
headerName: 'Full name',
description: 'This column has a value getter and is not sortable.',
sortable: false,
width: 160,
valueGetter: (params: any) =>
`${params.getValue('firstName') || ''} ${params.getValue('lastName') || ''}`,
},
];
const rows = [
{ id: 1, lastName: 'Snow', firstName: 'Jon', age: 35 },
{ id: 2, lastName: 'Lannister', firstName: 'Cersei', age: 42 },
{ id: 3, lastName: 'Lannister', firstName: 'Jaime', age: 45 },
{ id: 4, lastName: 'Stark', firstName: 'Arya', age: 16 },
{ id: 5, lastName: 'Targaryen', firstName: 'Daenerys', age: null },
{ id: 6, lastName: 'Melisandre', firstName: null, age: 150 },
{ id: 7, lastName: 'Clifford', firstName: 'Ferrara', age: 44 },
{ id: 8, lastName: 'Frances', firstName: 'Rossini', age: 36 },
{ id: 9, lastName: 'Roxie', firstName: 'Harvey', age: 65 },
];
function DataGridDemo() {
return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid rows={rows} columns={columns} pageSize={5} checkboxSelection />
</div>
);
}
ReactDOM.render(<DataGridDemo />, document.getElementById('app'));
package.json 在切换到 react-data-grid 之前
"dependencies": {
"@material-ui/core": "^4.11.4",
"@material-ui/data-grid": "^4.0.0-alpha.27",
"cors": "^2.8.5",
"express": "^4.17.1",
"mongodb": "^3.6.6",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"source-map-support": "^0.5.16",
"typescript": "^4.2.4"
},
"devDependencies": {
"@types/cors": "^2.8.10",
"@types/express": "^4.17.11",
"@types/mongodb": "^3.6.12",
"@types/node": "13",
"@types/react": "^17.0.4",
"@types/react-dom": "^17.0.3",
"@types/webpack-env": "^1.16.0",
"electron": "12.0.5",
"electron-builder": "^22.4.1",
"electron-webpack": "^2.8.2",
"electron-webpack-ts": "^4.0.1",
"webpack": "~4.42.1"
}