簡體   English   中英

材料表不會添加行

[英]Material-Table would not add row

React 是 JS 的新手。 嘗試使用帶有添加行按鈕的材料表。 添加行不會添加行。 一個刷新行被重置。 我很確定我在設置/更新 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;

提前致謝。

this.updateState 方法未綁定到 class。

您可以像這樣綁定構造函數,

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