Skip to content

Commit b35dd6b

Browse files
fix(routes/index): only render map client-side
This patch fixes a server-client error that was occurring because `react-simple-maps` does not fully support server-side rendering. I should fix this upstream, but this is a temporary fix to avoid surfacing the error message in my application. Ref: zcreativelabs/react-simple-maps#337
1 parent 83c0be6 commit b35dd6b

File tree

2 files changed

+57
-46
lines changed

2 files changed

+57
-46
lines changed

app/components/client-only.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { type PropsWithChildren, useState, useEffect } from 'react'
2+
3+
export function ClientOnly({ children }: PropsWithChildren) {
4+
const [mounted, setMounted] = useState(false)
5+
useEffect(() => setMounted(true), [])
6+
/* eslint-disable-next-line react/jsx-no-useless-fragment */
7+
return mounted ? <>{children}</> : null
8+
}

app/routes/_index/route.tsx

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515

1616
import { type loader as locationAPI } from 'routes/api.locations.$location'
1717

18+
import { ClientOnly } from 'components/client-only'
1819
import { LoadingLine } from 'components/loading-line'
1920
import { Button } from 'components/ui/button'
2021
import {
@@ -97,52 +98,54 @@ function Map({ className }: { className?: string }) {
9798
)
9899
return (
99100
<div className={cn('flex justify-center items-center', className)}>
100-
<ComposableMap
101-
className='object-scale-down max-w-full max-h-full'
102-
projectionConfig={{ scale: 147 }}
103-
width={800}
104-
height={400}
105-
>
106-
<Sphere
107-
id='sphere'
108-
fill='transparent'
109-
stroke='inherit'
110-
className='stroke-gray-200 dark:stroke-gray-800'
111-
strokeWidth={0.5}
112-
/>
113-
<Graticule
114-
className='stroke-gray-200 dark:stroke-gray-800'
115-
strokeWidth={0.5}
116-
/>
117-
<Geographies geography={geography}>
118-
{({ geographies }) =>
119-
geographies.map((geo) => (
120-
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
121-
<Geography
122-
key={(geo as { rsmKey: string }).rsmKey}
123-
geography={geo}
124-
className='fill-gray-900 dark:fill-gray-100'
125-
/>
126-
/* eslint-enable @typescript-eslint/no-unsafe-assignment */
127-
))
128-
}
129-
</Geographies>
130-
{Object.entries(LOCATION_TO_COORDINATES).map(
131-
([location, coordinates]) => {
132-
const stats = counts.find((c) => c.location === location)
133-
if (stats == null || !hasLocation(stats)) return null
134-
const radius = scale(stats.showsCount)
135-
return (
136-
<LocationMarker
137-
key={location}
138-
stats={stats}
139-
radius={radius}
140-
coordinates={coordinates}
141-
/>
142-
)
143-
},
144-
)}
145-
</ComposableMap>
101+
<ClientOnly>
102+
<ComposableMap
103+
className='object-scale-down max-w-full max-h-full'
104+
projectionConfig={{ scale: 147 }}
105+
width={800}
106+
height={400}
107+
>
108+
<Sphere
109+
id='sphere'
110+
fill='transparent'
111+
stroke='inherit'
112+
className='stroke-gray-200 dark:stroke-gray-800'
113+
strokeWidth={0.5}
114+
/>
115+
<Graticule
116+
className='stroke-gray-200 dark:stroke-gray-800'
117+
strokeWidth={0.5}
118+
/>
119+
<Geographies geography={geography}>
120+
{({ geographies }) =>
121+
geographies.map((geo) => (
122+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
123+
<Geography
124+
key={(geo as { rsmKey: string }).rsmKey}
125+
geography={geo}
126+
className='fill-gray-900 dark:fill-gray-100'
127+
/>
128+
/* eslint-enable @typescript-eslint/no-unsafe-assignment */
129+
))
130+
}
131+
</Geographies>
132+
{Object.entries(LOCATION_TO_COORDINATES).map(
133+
([location, coordinates]) => {
134+
const stats = counts.find((c) => c.location === location)
135+
if (stats == null || !hasLocation(stats)) return null
136+
const radius = scale(stats.showsCount)
137+
return (
138+
<LocationMarker
139+
key={location}
140+
stats={stats}
141+
radius={radius}
142+
coordinates={coordinates}
143+
/>
144+
)
145+
},
146+
)}
147+
</ComposableMap>
148+
</ClientOnly>
146149
</div>
147150
)
148151
}

0 commit comments

Comments
 (0)