Chi*_*ouS 7 javascript render typescript reactjs material-table
我有基本的反应示例用于学习,我在我的组件之一中使用材料表。每次我更改页面并重新打开它(卸载和安装组件)时,包含材质表的组件加载速度会更慢。我在下面分享我的代码。
import MaterialTable from 'material-table';
const columns = [
{ title: 'Id', field: 'id', hidden: true },
{ title: 'Username', field: 'username' },
{ title: 'Name', field: 'name' },
{ title: 'Phone', field: 'phone'}
];
const tableData = [
{
id: 1,
username: "User-1",
name: "name-1",
phone: "555 444 33 22"
},
{
id: 2,
username: "User-2",
name: "name-2",
phone: "111 222 33 44"
},
{
id: 3,
username: "User-3",
name: "name-3",
phone: "999 999 99 99"
}
];
const MTable = () => {
return (
<MaterialTable
title="Basic Search Preview"
columns={columns}
data={tableData}
options={{search: true }}
/>
)
}
export default MTable
Run Code Online (Sandbox Code Playgroud)
经过长时间的搜索,我没有找到任何解决方案,经过长时间的尝试,我只是更改了列定义的位置,如下所示。
const MTable = () => {
const columns = [
{ title: 'Id', field: 'id', hidden: true },
{ title: 'Username', field: 'username' },
{ title: 'Name', field: 'name' },
{ title: 'Phone', field: 'phone'}
];
return (
<MaterialTable
title="Basic Search Preview"
columns={columns}
data={tableData}
options={{search: true }}
/>
)
}
Run Code Online (Sandbox Code Playgroud)
这个改变解决了我的问题,但我真的想知道为什么会发生这种情况。当我在方法之外进行列定义时,为什么内存泄漏和渲染会减慢每个页面更改的速度。与此同时,当我进入方法时发生了什么变化?
material-tablecolumn.tableData
向列表的每一列添加一个属性columns
,然后有一个分配可以有效执行类似的操作(请参阅文件data-manager.js):
column[0].tableData.width = "calc(" + ... +
column[0].tableData.width + ... +
column[1].tableData.width + ... + ")"
column[1].tableData.width = "calc(" + ...
...
Run Code Online (Sandbox Code Playgroud)
因为列位于全局范围内,并且不会在每次卸载时被销毁,所以这使得字符串tableData.width
呈指数增长。我猜想花费的时间越来越长是因为越来越多的嵌套“calc()”调用。
我犹豫是否称这是材料表中的错误。
看起来材质表期望在每次渲染时创建列(并且不是持久的)。很公平,但对于习惯在React中工作的人来说,我至少会称之为意外行为,并且文档中应该对此有一个警告。我还认为即使那样,这也可以万无一失地实施。(如果有人不同意,我想在评论中阅读原因)
第一次安装组件时tableData.width
:
calc((100% - (0px +
calc((100% - (0px)) / 3) +
calc((100% - (0px)) / 3) +
calc((100% - (0px)) / 3)
)) / 3)
Run Code Online (Sandbox Code Playgroud)
卸载并第二次安装后,宽度tableData.width
为:
calc((100% - (0px +
calc((100% - (0px +
calc((100% - (0px + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3))) / 3) +
calc((100% - (0px + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3))) / 3) +
calc((100% - (0px + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3))) / 3)
)) / 3) +
calc((100% - (0px +
calc((100% - (0px + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3))) / 3) +
calc((100% - (0px + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3))) / 3) +
calc((100% - (0px + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3))) / 3)
)) / 3) +
calc((100% - (0px +
calc((100% - (0px + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3))) / 3) +
calc((100% - (0px + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3))) / 3) +
calc((100% - (0px + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3) + calc((100% - (0px)) / 3))) / 3)
)) / 3)
)) / 3)"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1210 次 |
最近记录: |