2

I am trying to toggle between light and dark theme on the runtime by following the documentation. When I toggle the theme within a component, the toggle function in App.js is called, but setting the theme does not have any effect.

In the component after creating theme-context.ts

import { ThemeContext } from '../../../theme-context';

themeContext.toggleTheme();

this is the App.js code

// Kick off initial async loading actions, like loading fonts and RootStore
  useEffect(() => {
    ;(async () => {
      setupRootStore().then(setRootStore)
    })()
  }, [])


  const [theme, setTheme] = React.useState('light');

  const toggleTheme = () => {
    const nextTheme = theme === 'light' ? 'dark' : 'light';
    setTheme(nextTheme);
    console.log("Toggle Theme pressed")
  };

  if (!rootStore) return null


  // otherwise, we're ready to render the app
  return (
    <RootStoreProvider value={rootStore}>
      <SafeAreaProvider initialSafeAreaInsets={initialWindowSafeAreaInsets}>
        <IconRegistry icons={EvaIconsPack} />
        <ThemeContext.Provider value={{ theme, toggleTheme }}>
        <ApplicationProvider {...eva} theme={eva.light}>
          {!rootStore.authStore.isLoggedIn && !startedPressed ?  <WelcomeSliderScreen pressed={getStartedPressed}></WelcomeSliderScreen> :
          <RootNavigator
            ref={navigationRef}
            initialState={initialNavigationState}
            onStateChange={onNavigationStateChange}
          />
          }
        </ApplicationProvider>
        </ThemeContext.Provider>
      </SafeAreaProvider>
    </RootStoreProvider>
  )
}

export default App

setTheme(nextTheme); is called but does not have any effect. Also setTheme('dark'); is not working.

kec334
  • 21
  • 2
  • Did you missed this step? Create theme-context.js file and paste the code below. https://akveo.github.io/react-native-ui-kitten/docs/guides/runtime-theming#create-theme-context – Gismo1337 Mar 09 '22 at 12:42
  • No I didn't missed it, I updated my code – kec334 Mar 09 '22 at 13:11

1 Answers1

1

Add an array with the theme state in the theme prop of ApplicationProvider as follows. Everything remains as is

theme={eva[theme]}
Kishan Bharda
  • 5,446
  • 3
  • 30
  • 57