nativewind icon indicating copy to clipboard operation
nativewind copied to clipboard

React Native Web error: Cannot manually set color scheme, as dark mode is type 'media'.

Open tomsjansons opened this issue 6 months ago • 10 comments

Describe the bug A new project created with pnpx rn-new@latest --nativewind produces an error in console while running in web.

Uncaught Error: Cannot manually set color scheme, as dark mode is type 'media'. Please use StyleSheet.setFlag('darkMode', 'class')
    at Object.set (AppEntry.bundle?platform=web&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.routerRoot=app&unstable_transformProfile=hermes-stable:42489:15)
    at MutationObserver.observe.attributes (AppEntry.bundle?platform=web&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.routerRoot=app&unstable_transformProfile=hermes-stable:42475:27)

Reproduction Repro: https://github.com/tomsjansons/nativewind-dark-mode

I've created the repro with the below:

pnpx rn-new@latest --nativewind
pnpx expo install react-dom react-native-web @expo/metro-runtime

There is no custom code in the repo with the exception of added classes bg-yellow-200 dark:bg-red-800 to check that the modes still work.

Run it with

pnpm i
pnpm run web

Expected behavior The error should not be thrown in console

Additional context I suspect that the problem is coming from https://github.com/nativewind/nativewind/blob/9e8ea10cee720f66f19bb26c365ef6d3570a9ba9/packages/react-native-css-interop/src/runtime/web/color-scheme.ts#L37

Image

I am not enirely sure if the bug is in the check and it could be fixed by doing if (darkModeFlag.startsWith('class')) or something similar or the bug is in the fact that --css-interop-darkMode is set to 'media'

Image

tomsjansons avatar May 21 '25 15:05 tomsjansons

I found out that you can use Appearance.setColorScheme from react-native and it'll work

makivlach avatar May 26 '25 09:05 makivlach

I get the same error when I use config in app.json "reactServerComponentRoutes": true

whiskeycola avatar May 28 '25 20:05 whiskeycola

I found out that you can use Appearance.setColorScheme from react-native and it'll work

Where?

rkahle avatar Jul 08 '25 14:07 rkahle

Where?

The suggestion here was that you'd be able to use Appearance.setColorScheme instead of relying on the browser to switch it for you when the user changes their color preference https://reactnative.dev/docs/appearance

example here

https://github.com/expo/expo/blob/136b77f18e2426e885bdd619effe181214c41172/apps/notification-tester/src/app/_layout.tsx#L14

the problem is that you'd need to do some wiring to listen to user's changes if you were to call setColorScheme manually and switch the theme and that seems a lot of work for something that does actually work out of the box.

my issue here is that it just logs the error in console even though everything works fine

tomsjansons avatar Jul 09 '25 12:07 tomsjansons

Same here (nativewind + expo-web)

kristian-nst avatar Aug 09 '25 03:08 kristian-nst

Hey all!

Set up a new project and found myself here, In my tailwind.config.js, adding darkMode: "class" fixed the issue for me.

Hope it helps!

jerrickhakim avatar Sep 15 '25 06:09 jerrickhakim

I hit the same issue, I'm using NativeWind 5 + Expo 0.54 in a yarn monorepo.

import { useColorScheme } from 'nativewind'
const { colorScheme, setColorScheme } = useColorScheme()

... onPress={() => setColorScheme(colorScheme === 'light' ? 'dark' : 'light')

And on web I get this error (ios works) Appearance.default.setColorScheme is not a function

Thank you

rborn avatar Sep 29 '25 07:09 rborn

have you tried adding darkMode: 'class' into tailwind.config.js like so

/** @type {import('tailwindcss').Config} */
module.exports = {
  darkMode: 'class',
  content: ['./App.{js,ts,tsx}', './components/**/*.{js,ts,tsx}'],

  presets: [require('nativewind/preset')],
  theme: {
    extend: {},
  },
  plugins: [],
};

sheriffMoose avatar Oct 01 '25 21:10 sheriffMoose

import { useColorScheme } from 'nativewind'
const { colorScheme, setColorScheme } = useColorScheme()

... onPress={() => setColorScheme(colorScheme === 'light' ? 'dark' : 'light')

And on web I get this error (ios works) Appearance.default.setColorScheme is not a function

I have the same issue with Nativewind v5 - you can't set darkMode: 'class' in tailwind.config.js because we don't use that file anymore!

I've added this to global.css but still get the same issue: @custom-variant dark (&:where(.dark, .dark *));

Directly calling Appearance.setColorScheme works for non-nativewind components

I can't figure out how to do manual theme changes on web

seanmozeik avatar Oct 02 '25 13:10 seanmozeik

I found out that react-native-web doesn't have a setColorScheme you'd need to polyfill it.

rborn avatar Oct 03 '25 06:10 rborn