React-native-calendars: Changing theme dynamically

Created on 8 Nov 2019  路  10Comments  路  Source: wix/react-native-calendars

is there any way to change theme dynamically depends on state or props?

I found #322 but still wonder any other possible ways to change theme dynamically

Feature request

Most helpful comment

Having the same problem. It's a slight hack but I solved the problem by setting the key prop based on the theme i.e

const [{key, theme}, setTheme] = useState({key: 'dark', theme: {...}})
...
<Calendar key={key} theme={theme} />

When the theme changes, so does the key, so the Calendar rerenders only when the theme changes.

All 10 comments

Maybe anyone has an idea how to work around that issue? I have failed miserably few times with different approaches (not involving source editing).

Unfortunately react-native-calendars is now the only one package which stops our app new release introducing Dark Mode support on iOS.

Also could an owner or contributor tell us how much effort and/or how difficult it will be to introduce dynamic styles? I'm open to help with development during next weekend if you're interested.

Having the same problem. It's a slight hack but I solved the problem by setting the key prop based on the theme i.e

const [{key, theme}, setTheme] = useState({key: 'dark', theme: {...}})
...
<Calendar key={key} theme={theme} />

When the theme changes, so does the key, so the Calendar rerenders only when the theme changes.

Having the same problem. It's a slight hack but I solved the problem by setting the key prop based on the theme i.e

const [{key, theme}, setTheme] = useState({key: 'dark', theme: {...}})
...
<Calendar key={key} theme={theme} />

When the theme changes, so does the key, so the Calendar rerenders only when the theme changes.

Yes, but it will re-render the calendar. As the styles are constructed in the constructor you can't change the theme.

I had achieved this by making changes in style creation

https://drive.google.com/open?id=1mZOVjOjuQdiesgnzvkxQXPd5LUpIFkkW

What exactly did you change?

@amol251, your link does not work :(

@amol251 Can your share your changes

Link: https://drive.google.com/open?id=1FRwJGiv-9_yNIutnqJ8ZLMb3ZzKIih2Y

This link is working, check this code

Do you have any idea how we can support it?
@ethanshar @emilisb

It's a little problematic.
The way the Calendar component was designed is so the theme prop is more of an initialTheme prop.
It is being used only when the component constructed.
This making it an un-controlled component (theme wise)

One solution is to use key (as already suggested here).
But since Calendar is a huge component it can heavily affect on performance.

Using getDerivedStateFromProps is impossible, because we're keeping the generated style as a class member (on this) and getDerivedStateFromProps is a static method.

The only right solution is to turn this prop (theme) to a controlled prop.
But unfortunately, this requires a lot of work and possibly introduce breaking changes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Yandamuri picture Yandamuri  路  4Comments

filippoitaliano picture filippoitaliano  路  3Comments

sommeshEwall picture sommeshEwall  路  3Comments

kewin1807 picture kewin1807  路  4Comments

idlework picture idlework  路  4Comments