stackoverflow, I'm really new to React, can you show me please, what I'm doing wrong, thank you!
This code is really simple, so I included all
class Buttons extends React.Component {
constructor(props) {
super(props);
this.state = {
totalPages: 0,
currentPage: 1
}
}
loadPage = (value) => {
let page = this.state.currentPage
if (this.props.value === "Previous Page"){
this.setState(state => ({
currentPage: page - 1
}));
page = this.state.currentPage
console.log(`Changing to -1, state.currentPage: ${this.state.currentPage}, page ${page}`)
}
if (this.props.value === "Next Page"){
this.setState(state => ({
currentPage: page + 1
}));
page = this.state.currentPage
console.log(`Changing to +1, state.currentPage: ${this.state.currentPage}, page ${page}`)
}
}
class Menu extends React.Component {
constructor(props) {
super(props);
this.state = {
page: 0,
totalPages: 0
}
}
render() {
return (
<div>
<nav aria-label="Page navigation example">
<ul className="pagination">
<Buttons value = "Next Page"/>
<Buttons value = "Previous Page"/>
</ul>
</nav>
</div>
);
}
This is a result, which I got from the Chrome console.
It looks like, React not changing the state, but making a new instance of the object, but I have no idea how to fix that.