Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: moved useeffect for theme provider (#21378)

Jonathan Summers-Muir41fe4a993b61
diff --git a/apps/studio/components/interfaces/Account/Preferences/ThemeSettings.tsx b/apps/studio/components/interfaces/Account/Preferences/ThemeSettings.tsx
index 13b4de36a59..9e4e9676728 100644
--- a/apps/studio/components/interfaces/Account/Preferences/ThemeSettings.tsx
+++ b/apps/studio/components/interfaces/Account/Preferences/ThemeSettings.tsx
@@ -1,6 +1,7 @@
-import { useTheme } from 'next-themes'
import Panel from 'components/ui/Panel'
import { BASE_PATH } from 'lib/constants'
+import { useTheme } from 'next-themes'
+import { useEffect, useState } from 'react'
import SVG from 'react-inlinesvg'
import {
Label_Shadcn_,
@@ -11,8 +12,22 @@ import {
} from 'ui'
const ThemeSettings = () => {
+ const [mounted, setMounted] = useState(false)
const { theme, setTheme } = useTheme()
+ /**
+ * Avoid Hydration Mismatch
+ * https://github.com/pacocoursey/next-themes?tab=readme-ov-file#avoid-hydration-mismatch
+ */
+ // useEffect only runs on the client, so now we can safely show the UI
+ useEffect(() => {
+ setMounted(true)
+ }, [])
+
+ if (!mounted) {
+ return null
+ }
+
function SingleThemeSelection() {
return (
<form>
diff --git a/apps/studio/components/interfaces/Account/Preferences/ThemeSettingsOld.tsx b/apps/studio/components/interfaces/Account/Preferences/ThemeSettingsOld.tsx
index e3900f5d501..a396c1bb562 100644
--- a/apps/studio/components/interfaces/Account/Preferences/ThemeSettingsOld.tsx
+++ b/apps/studio/components/interfaces/Account/Preferences/ThemeSettingsOld.tsx
@@ -2,10 +2,25 @@ import { useTheme } from 'next-themes'
import { IconMoon, IconSun, Listbox, Theme, themes } from 'ui'
import Panel from 'components/ui/Panel'
+import { useEffect, useState } from 'react'
const ThemeSettings = () => {
+ const [mounted, setMounted] = useState(false)
const { theme, resolvedTheme, setTheme } = useTheme()
+ /**
+ * Avoid Hydration Mismatch
+ * https://github.com/pacocoursey/next-themes?tab=readme-ov-file#avoid-hydration-mismatch
+ */
+ // useEffect only runs on the client, so now we can safely show the UI
+ useEffect(() => {
+ setMounted(true)
+ }, [])
+
+ if (!mounted) {
+ return null
+ }
+
return (
<Panel title={<h5 key="panel-title">Theme</h5>}>
<Panel.Content>
diff --git a/apps/studio/components/interfaces/Account/Preferences/ThemeSettingsWithPreferredTheme.tsx b/apps/studio/components/interfaces/Account/Preferences/ThemeSettingsWithPreferredTheme.tsx
index 6ced2a02cac..995dd12025a 100644
--- a/apps/studio/components/interfaces/Account/Preferences/ThemeSettingsWithPreferredTheme.tsx
+++ b/apps/studio/components/interfaces/Account/Preferences/ThemeSettingsWithPreferredTheme.tsx
@@ -1,23 +1,25 @@
-import { useTheme } from 'next-themes'
import Panel from 'components/ui/Panel'
import { BASE_PATH } from 'lib/constants'
-import { useState } from 'react'
+import { useTheme } from 'next-themes'
+import { useEffect, useState } from 'react'
import SVG from 'react-inlinesvg'
import {
Label_Shadcn_,
- prefixedThemes,
- RadioGroup_Shadcn_,
RadioGroupLargeItem_Shadcn_,
- Select_Shadcn_,
+ RadioGroup_Shadcn_,
SelectContent_Shadcn_,
SelectItem_Shadcn_,
SelectTrigger_Shadcn_,
SelectValue_Shadcn_,
- singleThemes,
+ Select_Shadcn_,
Theme,
+ prefixedThemes,
+ singleThemes,
} from 'ui'
const ThemeSettings = () => {
+ const [mounted, setMounted] = useState(false)
+
// @ts-expect-error // remove this ts error if you use this file
const { theme, setTheme, setSystemPrefixResolve, themePrefix } = useTheme()
const [mode, setMode] = useState<'system' | 'single-theme'>(
@@ -25,6 +27,19 @@ const ThemeSettings = () => {
)
const [prefixedTheme, setPrefixedThemeState] = useState(themePrefix === '' ? 'dark' : themePrefix)
+ /**
+ * Avoid Hydration Mismatch
+ * https://github.com/pacocoursey/next-themes?tab=readme-ov-file#avoid-hydration-mismatch
+ */
+ // useEffect only runs on the client, so now we can safely show the UI
+ useEffect(() => {
+ setMounted(true)
+ }, [])
+
+ if (!mounted) {
+ return null
+ }
+
function SingleThemeSelection() {
return (
<form>
diff --git a/packages/common/Providers.tsx b/packages/common/Providers.tsx
index 657113430de..dcb2d3e5162 100644
--- a/packages/common/Providers.tsx
+++ b/packages/common/Providers.tsx
@@ -1,20 +1,9 @@
import { ThemeProvider as NextThemesProvider } from 'next-themes'
import { type ThemeProviderProps } from 'next-themes/dist/types'
-import { useEffect, useState } from 'react'
export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
- const [mounted, setMounted] = useState(false)
// @ts-ignore next-themes is old :/
- // useEffect only runs on the client, so now we can safely show the UI
- useEffect(() => {
- setMounted(true)
- }, [])
-
- if (!mounted) {
- return null
- }
-
return (
<NextThemesProvider {...props} themes={['dark', 'light', 'deep-dark']}>
{children}