Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Not re-rendering on theme prop changes #2539

Open
aminmeshk opened this issue Oct 9, 2024 · 2 comments
Open

[BUG] Not re-rendering on theme prop changes #2539

aminmeshk opened this issue Oct 9, 2024 · 2 comments

Comments

@aminmeshk
Copy link

Description

When the theme prop or the values inside the theme object changes, calendar doesn't update the UI based on the new theme values. In my case, when the device scheme changes from light to dark, I change the theme prop but it doesn't update the calendar. I have to do a full unmount & mount of the <Calendar /> component in order to see the theme changes.

Expected Behavior

When theme prop changes, <Calendar /> component should re-render with the new changes

Observed Behavior

What actually happened when you performed the above actions?
When I change the dayTextColor value inside the theme prop from white to black, it stays white until I unmount & re-mount the component.

Environment

Please run these commands in the project folder and fill in their results:

Also specify:

  1. Device/emulator/simulator & OS version:
    iOS Simulator - iPhone 16 Pro - iOS 18.0

Reproducible Demo

import React from "react";
import { useColorScheme } from "react-native";
import { Calendar } from "react-native-calendars";

const App = () => {
	const scheme = useColorScheme();

	return (
		<Calendar
			onDayPress={day => {
				console.log("selected day", day);
			}}
			theme={{
				calendarBackground: "transparent",
				dayTextColor: scheme === "dark" ? "white" : "black",
			}}
		/>
	);
};

Screenshots

Current:
Current

What should be (I fixed it with a patch):
Fixed

Solution

This problem happens because theme prop is used as initialValue of useRef. So when the theme prop changes, the changes doesn't affect the component. For example, take a look at day/basic component:

...
const BasicDay = (props) => {
    const { theme, date, ... } = props;
    const style = useRef(styleConstructor(theme));
...

To fix this I added a useEffect that when the theme prop changes, it updates the style ref:

...
const BasicDay = (props) => {
    const { theme, date, ... } = props;
    const style = useRef(styleConstructor(theme));
    useEffect(() => {
        style.current = styleConstructor(theme);
    }, [theme]);
...

With this addition, the issue is fixed as you can see in the second GIF above.
I will create a PR for it.

@aminmeshk
Copy link
Author

I added PR to fix this:
#2540

@oogunjob
Copy link

oogunjob commented Nov 9, 2024

This helped a lot, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants