class Demo extends Component {
constructor(){
super()
this.state = {
dataSource:[],
columns:[{ key: "data", title: "", dataIndex: "data"}],
}
}
onClick = () => {
this.state.dataSource.push({key:"1",data:"1"})
console.log(this.state.dataSource)
}
render(){
return(
<div>
<Table dataSource={this.state.dataSource} columns={this.state.columns}></Table>
<Button onClick={this.onClick}>add</Button>
</div>
)
}
}
Boss look at the code
purpose: add a set of data to the table in the click button page
to make the react render the page you have to use the setstate method
but the value returned by the push method is the length of the array, not the data of the array
I can"t directly setState ({dataSource:this.state.dataSource.push ({key:"1",) Data:"1"})
so how do I use setState? how do I get react to render data to the table when inserting new values into the array?-sharp-sharp-sharp topic description