I've created the component, which passes the function to change its state to the child.
//parent component
setSubject = (id) => {
this.setState({
currentSubject: id
});
}
<Subjects authToken = {this.state.authToken} subjects = {this.state.subjects} setSubject = {this.setSubject} />
//child component
<li onClick={() => this.props.setSubject(subject.id)}>Egzamino programa</li>
That state is passed to another component.
<Sections authToken = {this.state.authToken} subject = {this.state.currentSubject} />
From there I am using componentDidUpdate() method to handle this change:
componentDidUpdate() {
if (this.props.subject) {
axios.get(`http://localhost:3000/api/subjects/${this.props.subject}/sections?access_token=${this.props.authToken}`)
.then(response => {
this.setState({
sections: response.data
})
}).catch(err => {
console.log(err)
})
}
}
Everything works as expected, BUT when I try to console.log something in Sections component after I've set currentSubject through Subjects component, that console.log executes endless number of times (so is get request, i guess...) It is not goot, is it? And I cannot understand why this happens..