I want to render different data on my Line Chart Widget component depending on the user input. So I created a select tag:
<select id="lang" onChange={this.handleLineChart} value={this.state.value}>
<option value="indiaData">All India</option>
<option value="stateData">States</option>
</select>
constructor
constructor(props){
super(props);
this.state = {
value: 'indiadata'
}
this.handleLineChart = this.handleLineChart.bind(this);
}
Onchange Event
handleLineChart(event) {
this.setState({value: event.target.value});
}
My component
{
this.state.value === "indiaData" ?
<LineChartWidget data={indiaData} /> :
<LineChartWidget data={stateData} />
}
Both my data "indiaData" and "stateData" are arrays of objects and I am fetching 'em through API. By default, it works perfectly while rendering indiaData but when I change the input to States, it does not show any data on the line chart, though I can log it in my console (on change of input). There are no issues with the data.
Also, if I reverse my default state value to
this.state = {
value: 'statedata'
}
and my component to exactly opposite
{
this.state.value === "stateData" ?
<LineChartWidget data={stateData} /> :
<LineChartWidget data={indiaData} />
}
Then, it will render stateData and not indiaData on changing the input.
There is one more scenario, so when my component looks something like this
{
this.state.value === "stateData" ?
<LineChartWidget data={stateData} /> :
<div>Test</div>
}
It works perfectly with the div but not with the second Linechart.
Shortly, my issue here is that it only renders the first condition and not the second one, keeping in mind that there is no issue with the data.