I am new to ReactJS. When I click on the button, the call will go from createTablesam.js
to index.js
to update the object. the object is updated successfully in setState
but not updating in UI. If I again the button again the object is updating but UI is not updating.
const PureComponent = React.PureComponent;
class Index extends PureComponent {
constructor() {
super();
this.state = {
userValues: [
{
name: "Mani",
age: 23,
skill: "Java"
},
{
name: "Vel",
age: 23,
skill: "react"
}
]
};
this.addRow = this.addRow.bind(this);
}
addRow() {
let newdetails = {
name: "ManiVEL",
age: 25,
skill: "REACT JS"
};
let userValues = this.state.userValues;
userValues.push(newdetails);
this.setState({
userValues
});
}
render() {
return (
<CreateTablesam
addtable={this.addRow}
tabelDeatils={this.state.userValues}
></CreateTablesam>
);
}
}
class CreateTablesam extends PureComponent {
state = {
tableCopy: this.props.tabelDeatils
};
render() {
const createRow = this.state.tableCopy.map(per => (
<tr key={per.name}>
<td>{per.name}</td>
<td>{per.age}</td>
<td>{per.skill}</td>
</tr>
));
return (
<div>
<table>
<thead>
<tr>
<td>Name</td>
<td>Age</td>
<td>skills</td>
</tr>
</thead>
<tbody>{createRow}</tbody>
</table>
<button onClick={this.props.addtable}>CLICK</button>
</div>
);
}
}
ReactDOM.render(<Index />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
what mistake have I done?