I created a LineChart
using recharts and I'm pulling data using axios
from a db.json
file hosted locally by json-server. Every time I load the page or reload the chart doesn't show up. Only when I hit ctrl+s
to save my file and trigger re-render does the chart show up. How do I get the chart to show up on initial page load?
I used Vite
with SWC
to initialize the project and versions are: react@18.2.0
, vite@4.4.3
, recharts@2.7.2
Chart code:
<ResponsiveContainer width="100%" height={270}>
<LineChart
width={600}
height={300}
data={monthlyData}
margin={{
left: -20,
}}
>
<CartesianGrid vertical={false} />
<Tooltip />
<XAxis axisLine={false} dataKey="name" tickSize={0} tickMargin={10} />
<YAxis
axisLine={false}
domain={[0, maxY]}
tickCount={maxY / 100 + 1}
tickSize={0}
tickMargin={10}
/>
<Line
type="monotone"
dataKey="user"
strokeWidth={2}
stroke="#9BDD7C"
dot={false}
/>
<Line
type="monotone"
dataKey="guest"
strokeWidth={2}
stroke="#E9A0A0"
dot={false}
/>
</LineChart>
</ResponsiveContainer>
This is how I'm fetching the data:
const getActivity = async (index) => {
const res = await api.get("/activity");
const data = res?.data;
setYearlyData(data);
setMonthlyData(yearlyData[index]);
};
useEffect(() => {
getActivity(0);
}, []);