Skip to content

Commit 4481436

Browse files
committed
feat: add useIsClient custom hook
1 parent ce96c87 commit 4481436

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/components/EnergyConsumptionChart.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from "react"
2-
import { useEffect, useState } from "react"
32
import {
43
BarElement,
54
CategoryScale,
@@ -25,6 +24,8 @@ import type { Lang } from "@/lib/types"
2524
import { wrapLabel } from "@/lib/utils/charts"
2625
import { isLangRightToLeft } from "@/lib/utils/translations"
2726

27+
import { useIsClient } from "@/hooks/useIsClient"
28+
2829
// ChartDataLabels required to display y-labels on top of bars
2930
ChartJS.register(
3031
CategoryScale,
@@ -37,18 +38,9 @@ ChartJS.register(
3738
const EnergyConsumptionChart = () => {
3839
const { t } = useTranslation("page-what-is-ethereum")
3940
const { locale } = useRouter()
41+
const isClient = useIsClient()
4042
const isRtl = isLangRightToLeft(locale as Lang)
4143

42-
const useIsClient = () => {
43-
const [isClient, setClient] = useState(false)
44-
45-
useEffect(() => {
46-
setClient(true)
47-
}, [])
48-
49-
return isClient
50-
}
51-
5244
// chart rawData, according to different breakpoints
5345
const rawData = useBreakpointValue({
5446
base: [
@@ -251,7 +243,7 @@ const EnergyConsumptionChart = () => {
251243
mb={{ base: 4, md: 0 }}
252244
>
253245
{/* TODO: isRtl ? data?.reverse() : data */}
254-
{useIsClient() && (
246+
{isClient && (
255247
<Bar options={chartOptions} data={chartData} updateMode="none" />
256248
)}
257249
</Box>

src/hooks/useIsClient.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { useEffect, useState } from "react"
2+
3+
export const useIsClient = () => {
4+
const [isClient, setClient] = useState(false)
5+
6+
useEffect(() => {
7+
setClient(true)
8+
}, [])
9+
10+
return isClient
11+
}

0 commit comments

Comments
 (0)