javascript - 材料表不会添加行
问题描述
React 对 JS 来说相当新。尝试将 Material-table 与添加行按钮一起使用。添加行不会添加行。一刷新行被重置。我很确定我在设置/更新状态时做错了。
export default function App() {
return (
<div className="App">
<Tabl
obj={{
a: "a",
items: [{ x: 1 }, { x: 2 }, { x: 3 }]
}}
/>
</div>
);
}
class Tabl extends Component {
constructor(props) {
super(props);
this.state = {
obj: props.obj
};
console.log(JSON.stringify(this.state.obj));
}
updateState(newData) {
this.setState({
obj: [...this.state.obj.items, newData]
});
}
render() {
const currObj = this.state.obj;
const column = [
{
title: "a",
field: "x"
}
];
const tableIcons = {
Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />),
Check: forwardRef((props, ref) => <Check {...props} ref={ref} />),
Clear: forwardRef((props, ref) => <Clear {...props} ref={ref} />)
};
return (
<MaterialTable
data={currObj.items}
columns={column}
icons={tableIcons}
options={{
search: false,
paging: false
}}
editable={{
onRowAdd: (newData) =>
new Promise((resolve, reject) => {
setTimeout(() => {
this.updateState(newData);
resolve();
}, 1000);
})
}}
/>
);
}
}
export default Tabl;
提前致谢。
解决方案
this.updateState 方法未绑定到类。
您可以像这样在构造函数中绑定,
constructor(props) {
super(props);
this.state = {
obj: props.obj
};
console.log(JSON.stringify(this.state.obj));
this.updateState= this.updateState.bind(this);
}
推荐阅读
- java - JavaFX:使用 Point2D 和 Line 进行寻路
- excel - 根据选择的约束类型调用不同的求解器
- c# - 用于登录物联网设备的简单 C# Web API
- javascript - 同时检查来自多个数组的布尔值,无需多个 if 语句
- ios - 使用 CloudKit 处理完成
- solr - Solr - 仅约束更多类似结果
- python - 如何加快我的 MySQL (InnoDB) 插入速度?
- pdf - PDF文件中较低索引字母的大小问题,例如:j,p,q,g等
- windows-installer - 更改项目设置 MSI 的产品代码和名称
- python - 替换列表 Python 中的所有元素