I am using React Apex chart library in my project.I’ve figured that the library charts have a small delay before rendering on initial mount.
This issue harms the UX because the component that renders ApexCharts won’t be displayed until after the delay.
JavaScript
x
36
36
1
import React from "react";
2
import Chart from "react-apexcharts";
3
4
function MixedChart() {
5
const data = {
6
options: {
7
chart: {
8
id: "basic-bar"
9
},
10
xaxis: {
11
categories: [1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999]
12
}
13
},
14
series: [
15
{
16
name: "series-1",
17
data: [30, 40, 45, 50, 49, 60, 70, 91]
18
}
19
]
20
};
21
22
return (
23
<div className="mixed-chart">
24
<Chart
25
options={data.options}
26
series={data.series}
27
type="bar"
28
width="500"
29
/>
30
</div>
31
);
32
33
}
34
35
export default MixedChart;
36
Is there a way to fix this ?
Advertisement
Answer
I figured out the solution.
To avoid the app rendering nothing in that delay and have a better UX, you can use a set timeout inside useEffect hook to update a state after the chart’s initial mount.
JavaScript
1
20
20
1
const [dispaly, setDisplay] = useState(false);
2
3
useEffect(() => {
4
setTimeout(() => setDisplay(true), 1);
5
}, [])
6
7
if(!display) {
8
return <></>;
9
}
10
11
return (
12
<div className="mixed-chart">
13
<Chart
14
options={data.options}
15
series={data.series}
16
type="bar"
17
width="500"
18
/>
19
</div>);
20