How does React insert data into an array and call render to render the page

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

Apr.01,2021

this.setState({
    dataSource:[{key:'1',data:'1'}]
})

you still have a lot of knowledge to make up. The push in
js modifies the original array and does not return a new array.
if any value of props and state in react changes, it will be render.
so your code should be as follows:

  

use this.setState () to set state , direct assignment is not recommended

Menu