refactor mobile web color hydration

Replace the hydration state effect with useSyncExternalStore so the web color-scheme hook keeps its static-render fallback without triggering the set-state-in-effect lint warning.
pull/19545/head
Ryan Vogel 2026-03-30 08:28:25 -04:00
parent f8f986536b
commit df3276fc87
1 changed files with 18 additions and 10 deletions

View File

@ -1,21 +1,29 @@
import { useEffect, useState } from 'react';
import { useColorScheme as useRNColorScheme } from 'react-native';
import { useSyncExternalStore } from "react"
import { useColorScheme as useRNColorScheme } from "react-native"
function subscribe() {
return () => {}
}
function getSnapshot() {
return true
}
function getServerSnapshot() {
return false
}
/**
* To support static rendering, this value needs to be re-calculated on the client side for web
*/
export function useColorScheme() {
const [hasHydrated, setHasHydrated] = useState(false);
const hasHydrated = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)
useEffect(() => {
setHasHydrated(true);
}, []);
const colorScheme = useRNColorScheme();
const colorScheme = useRNColorScheme()
if (hasHydrated) {
return colorScheme;
return colorScheme
}
return 'light';
return "light"
}