Skip to content

Commit

Permalink
fix(routes/index): only render map client-side
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nicholaschiang committed Aug 10, 2023
1 parent 9165fc2 commit a279aa4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 46 deletions.
7 changes: 7 additions & 0 deletions app/components/client-only.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { type PropsWithChildren, useState, useEffect } from 'react'

export function ClientOnly({ children }: PropsWithChildren) {
const [mounted, setMounted] = useState(false)
useEffect(() => setMounted(true), [])
return mounted ? <>{children}</> : null

Check failure on line 6 in app/components/client-only.tsx

View workflow job for this annotation

GitHub Actions / ESLint

Fragments should contain more than one child - otherwise, there’s no need for a Fragment at all
}
95 changes: 49 additions & 46 deletions app/routes/_index/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {

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

import { ClientOnly } from 'components/client-only'
import { LoadingLine } from 'components/loading-line'
import { Button } from 'components/ui/button'
import {
Expand Down Expand Up @@ -97,52 +98,54 @@ function Map({ className }: { className?: string }) {
)
return (
<div className={cn('flex justify-center items-center', className)}>
<ComposableMap
className='object-scale-down max-w-full max-h-full'
projectionConfig={{ scale: 147 }}
width={800}
height={400}
>
<Sphere
id='sphere'
fill='transparent'
stroke='inherit'
className='stroke-gray-200 dark:stroke-gray-800'
strokeWidth={0.5}
/>
<Graticule
className='stroke-gray-200 dark:stroke-gray-800'
strokeWidth={0.5}
/>
<Geographies geography={geography}>
{({ geographies }) =>
geographies.map((geo) => (
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
<Geography
key={(geo as { rsmKey: string }).rsmKey}
geography={geo}
className='fill-gray-900 dark:fill-gray-100'
/>
/* eslint-enable @typescript-eslint/no-unsafe-assignment */
))
}
</Geographies>
{Object.entries(LOCATION_TO_COORDINATES).map(
([location, coordinates]) => {
const stats = counts.find((c) => c.location === location)
if (stats == null || !hasLocation(stats)) return null
const radius = scale(stats.showsCount)
return (
<LocationMarker
key={location}
stats={stats}
radius={radius}
coordinates={coordinates}
/>
)
},
)}
</ComposableMap>
<ClientOnly>
<ComposableMap
className='object-scale-down max-w-full max-h-full'
projectionConfig={{ scale: 147 }}
width={800}
height={400}
>
<Sphere
id='sphere'
fill='transparent'
stroke='inherit'
className='stroke-gray-200 dark:stroke-gray-800'
strokeWidth={0.5}
/>
<Graticule
className='stroke-gray-200 dark:stroke-gray-800'
strokeWidth={0.5}
/>
<Geographies geography={geography}>
{({ geographies }) =>
geographies.map((geo) => (
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
<Geography
key={(geo as { rsmKey: string }).rsmKey}
geography={geo}
className='fill-gray-900 dark:fill-gray-100'
/>
/* eslint-enable @typescript-eslint/no-unsafe-assignment */
))
}
</Geographies>
{Object.entries(LOCATION_TO_COORDINATES).map(
([location, coordinates]) => {
const stats = counts.find((c) => c.location === location)
if (stats == null || !hasLocation(stats)) return null
const radius = scale(stats.showsCount)
return (
<LocationMarker
key={location}
stats={stats}
radius={radius}
coordinates={coordinates}
/>
)
},
)}
</ComposableMap>
</ClientOnly>
</div>
)
}
Expand Down

0 comments on commit a279aa4

Please sign in to comment.