class MyComponent extends React.component {
constructor() {
this.state = {
counter: 0,
items: [1, 2]
};
}
increase(this) {
this.state.counter++;
}
addItem() {
this.state.items = this.state.items.push(this.state.items.length+1);
}
render() {
<div class="data">
<div>{this.state.counter}</div>
<ul>
{this.state.items.map(item => (
<li key={item}>element {item}</li>
))}
</ul>
</div>
<div class="actions">
<button type="button" onclick={this.increase}>zwiększ</button>
<button type="button" onclick={this.addItem}>dodaj</button>
</div>
}
}
ReactDOM.render(<MyComponent>, document.querySelector("body"));
this.state.counter++;
this.state.items = this.state.items.push(this.state.items.length+1);
this.setState({counter: this.state.counter+1});
this.setState({items: ...this.state.items, this.state.items.length+1});