This the code I am using. The problem is that once the Back button get disabled it does not get enabled when I click the next button. Can someone help me figuring out what am I doing wrong here?
class App extends React.Component { constructor(props) { super(props) this.state = { data: [], start: 1, disabledNext: false, disabledBack: false
}
}
componentDidMount() {
this.displayImages(this.state.start)
}
displayImages(a){
axios
.get(`https://jsonplaceholder.typicode.com/photos/?_start=${a}&_limit=5`)
.then(res => {
this.setState({
data : res.data
});
})
}
handleBack = () => {
let prev = this.state.start - 5
let disabledBack = false
if (prev <= 1){
prev = 1
disabledBack = true
}
this.setState({ start: prev , disabledBack: disabledBack })
this.displayImages(prev)
};
handleNext = () => {
let newStart = this.state.start + 5
this.setState({ start: newStart })
this.displayImages(newStart)
};
render() {
const { data , disabledBack } = this.state
return(
<div>
{
data.map(item => {
return(
<img src={item.thumbnailUrl} alt="img"/>
)
})
}
<br></br> <br></br>
<button type="button" onClick={this.handleBack} disabled={disabledBack}>Back</button>
<button type="button" onClick={this.handleNext} >Next</button>
</div>
)
}
}