I’ve built a coronavirus table and whenever someone clicks on the name of the particular country modal pops up with active cases chart. I realized that it might be an issue with the Modal Component imported from Bootstrap(but not quite sure). When I set the animation to false chart doesn’t show data on every modal opening. When the animation props are not included data sometimes is not loaded. Closing and reopening a couple of times does a trick tho.
JavaScript
x
62
62
1
<Modal show={show} onHide={handleClose}
2
animation={false}
3
size="lg"
4
aria-labelledby="contained-modal-title-vcenter"
5
centered />
6
7
8
const Chart = ({CountryName}) => {
9
const[data, setData] = useState({});
10
let caseDate = [];
11
let active = [];
12
let confirmed = [];
13
let deaths = [];
14
let caseDatesSubstracted = [];
15
16
const activeChart = () => {
17
setData({
18
labels: caseDatesSubstracted,
19
datasets: [
20
{
21
label: 'Active Cases',
22
data: active,
23
backgroundColor: [
24
['black']
25
],
26
}
27
]
28
})
29
}
30
useEffect(() => {
31
const loadData = async() => {
32
await fetch(`https://api.covid19api.com/total/dayone/country/${CountryName}`)
33
.then(response => response.json())
34
.then(data => {
35
for(const dataObj of data) {
36
console.log(data)
37
caseDate.push(dataObj.Date);
38
active.push(dataObj.Active);
39
confirmed.push(dataObj.Confirmed)
40
deaths.push(dataObj.Deaths)
41
}
42
for(let i = 0; i < caseDate.length; i++){
43
caseDatesSubstracted.push(caseDate[i].substring(0, caseDate[i].length-10));
44
}
45
})
46
}
47
loadData();
48
activeChart();
49
confirmedChart();
50
deathChart();
51
}, []);
52
53
return(
54
<div className="chart">
55
<h1 style={{margin: '50px 0'}}>Active Cases</h1>
56
<Line
57
data={data}
58
/>
59
</div>
60
)
61
}
62
Advertisement
Answer
try moving everything into useEffect like this. do not mix state and “let” variables.
I’m not sure what these 2 functions do, but it’s not advisable to those variables inside the function.
JavaScript
1
3
1
confirmedChart();
2
deathChart();
3
try these.
JavaScript
1
56
56
1
const Chart = ({ CountryName }) => {
2
const [data, setData] = useState({});
3
4
5
const activeChart = (caseDatesSubstracted) => {
6
//if u want to separate, then pass in the arg.
7
setData({ . }) // now u can use caseDatesSubstracted here.
8
}
9
10
11
12
useEffect(() => {
13
const loadData = async() => {
14
await fetch(`https://api.covid19api.com/total/dayone/country/${CountryName}`)
15
.then(response => response.json())
16
.then(data => {
17
//do not declare them outside useEffect as they get re-initialize on every render.
18
let caseDate = [];
19
let active = [];
20
let confirmed = [];
21
let deaths = [];
22
let caseDatesSubstracted = [];
23
24
for (const dataObj of data) {
25
console.log(data)
26
caseDate.push(dataObj.Date);
27
active.push(dataObj.Active);
28
confirmed.push(dataObj.Confirmed)
29
deaths.push(dataObj.Deaths)
30
}
31
for (let i = 0; i < caseDate.length; i++) {
32
caseDatesSubstracted.push(caseDate[i].substring(0, caseDate[i].length - 10));
33
}
34
35
//set ur state here.
36
/* alternatively, use activeChart(caseDatesSubstracted) by passing in the variable */
37
setData({
38
labels: caseDatesSubstracted,
39
datasets: [{
40
label: 'Active Cases',
41
data: active,
42
backgroundColor: [
43
['black']
44
],
45
}]
46
})
47
})
48
}
49
loadData();
50
confirmedChart(); //not good, move them. follow what I did for activeChart()
51
deathChart(); // not good, move them. follow what I did for activeChart()
52
}, []);
53
54
55
}
56