React native 如何在react native中获得正确的配色方案?

React native 如何在react native中获得正确的配色方案?,react-native,expo,darkmode,React Native,Expo,Darkmode,在我用Expo CLI创建的新react本机应用程序中,我使用一个自定义挂钩来支持带tailwindcss的黑暗模式。具有暗模式逻辑的TailwindProvider如下所示: consttailwindprovider:React.FC=({children})=>{ const[currentColorScheme,setcurrentColorScheme]=useState(外观.getColorScheme()); log(“CurrentColorScheme:”,CurrentCo

在我用Expo CLI创建的新react本机应用程序中,我使用一个自定义挂钩来支持带tailwindcss的黑暗模式。具有暗模式逻辑的TailwindProvider如下所示:

consttailwindprovider:React.FC=({children})=>{
const[currentColorScheme,setcurrentColorScheme]=useState(外观.getColorScheme());
log(“CurrentColorScheme:”,CurrentColorScheme);
const isDarkMode=currentColorScheme==“暗”;
常量tw=(类:字符串)=>tailwind(handlethemelclasses(类,isDarkMode))
返回{children};
}
如您所见,我使用
react native
中的
Appearance.getColorScheme()
方法来获取当前的配色方案。但无论是在iOS还是Android上,无论是在调试模式还是在生产模式下,我都会看到
亮的
,而不是
暗的


如何解决此问题?

useState(Appearance.getColorScheme())的问题是,在加载应用程序时只会检查一次。您可以尝试使用挂钩来保持最新:

// ...
import { useColorScheme } from 'react-native';

const TailwindProvider: React.FC = ({ children }) => {
  const currentColorScheme = useColorScheme();
  console.log({ currentColorScheme });
  const isDarkMode = currentColorScheme === "dark";
  const tw = (classes: string) => tailwind(handleThemeClasses(classes, isDarkMode));
  return <TailwindContext.Provider value={{ currentColorScheme, tw, isDarkMode }}>{children}</TailwindContext.Provider>;
}
/。。。
从“react native”导入{useColorScheme};
const TailwindProvider:React.FC=({children})=>{
const currentColorScheme=useColorScheme();
log({currentColorScheme});
常量IsDakMode=currentColorScheme==“暗”;
const tw=(classes:string)=>tailwind(handlethemelclasses(classes,isDarkMode));
返回{children};
}