I am setting the state of a state variable in reactjs when a press a button. I call the following procedure (which is actually being called).
nextSuggestion() {
console.log(this.state.suggestions_index);
this.state.suggestions_index = this.state.suggestions_index + 1;
console.log(this.state.suggestions_index);
}
The above function is called and the page is not rerendered, even though I have the following componentDidMount():
async componentDidMount(){
const query = new URLSearchParams(this.props.location.search);
const token = query.get('token');
const url = "http://www.mocky.io/v2/" + token;
this.setState({url_prod: url});
const response = await fetch(url);
const data = await response.json();
this.setState({produto: data.suggestions[this.state.suggestions_index], loading_produto: false})
this.setState({alternativas: data.choices, loading_alternativas: false})
this.setState({atributos: data.suggestions[this.state.suggestions_index].product_data.attributes, loading_atributos: false})
}
The button is created through this code in render() function:
if(!this.state.loading_alternativas){
this.texts = lista_alternativas.map((text, key) => {
return <div id="center-button"><button type="button" className="btn btn-primary" key={text.id} onClick={this.nextSuggestion.bind(this)}>{text.text}</button></div>
});
}
the state:
state = {
produto: null,
alternativas: null,
atributos: [],
suggestions_index: 0,
loading_produto: true,
loading_alternativas: true,
loading_atributos: true,
showPopupDescProd: false,
showPopupAnswer: false,
url_prod: null,
};
What am I missing to make it work?