First of all, I'm sorry for such a basic question, but I just started learning React from its official documentation.
Initial state of my app
I want to update the state of the topicTitle, topicContent and level when I click on the button 'Continue'. It should do so by calling the function NextTopic
and incrementing let level
in it. Then setting this.state.level
to the value of the incremented variable level
. After that the switch case should check the value of this.state.level
and if it equals 1, update the state of topicName and topicContent.
This is what my actually happens when my code runs
import React, { Component } from 'react';
export default class Sidebar extends Component {
//default states
constructor() {
super();
this.state = {
level: 0,
topicTitle: 'Topic Name',
topicContent: 'Content!!'
}
}
render() {
const NextTopic = (e) => {
//increment level
let level;
level++;
//set state of level to incremented let level
this.setState({level: level});
//check level of state and update topic name- and content state
switch(this.state.level) {
case 1:
return this.setState({topicTitle: 'new Topic Name', topicContent: 'new Content!!'});
}
}
//Sidebar Component
return (
<div>
<h1>{this.state.topicTitle}</h1>
<p>{this.state.topicContent}</p>
<h2>Level: {this.state.level}</h2>
<button id='next' onClick={NextTopic}>Continue</button>
</div>
)
}
};