简体   繁体   English

材料表不会添加行

[英]Material-Table would not add row

Fairly new to JS, React. React 是 JS 的新手。 Trying to use Material-table with a add row button.尝试使用带有添加行按钮的材料表。 Add row would not add the row.添加行不会添加行。 One refreshing the rows are reset.一个刷新行被重置。 I'm pretty sure I'm doing something wrong with setting/ updating the state.我很确定我在设置/更新 state 时做错了什么。

 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;

Thanks in advance.提前致谢。

this.updateState method not binding to class. this.updateState 方法未绑定到 class。

You can bind in constructor like this,您可以像这样绑定构造函数,

constructor(props) {
    super(props);
    this.state = {
      obj: props.obj
    };
    console.log(JSON.stringify(this.state.obj));
    this.updateState= this.updateState.bind(this);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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