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

React Compiler prevents theme updates in components with style prop when using themes #368

Open
Ucekay opened this issue Nov 29, 2024 · 3 comments
Labels
good first issue Good for newcomers
Milestone

Comments

@Ucekay
Copy link

Ucekay commented Nov 29, 2024

Description

When using React Compiler, theme updates don't work in components that:

  1. Use themes
  2. Accept a style prop

Example of affected component:

import { View, type ViewProps } from 'react-native';

import {
	StyleSheet,
} from 'react-native-unistyles';

export const UnistyledView =(props: ViewProps) => {
	const { style, ...otherProps } = props;
	return <View style={[style, styles.container]} {...otherProps} />;
};

const styles = StyleSheet.create((theme) => ({
	container: {
		backgroundColor: theme.colors.background,
	},
}));

Details:

  • Theme updates stop working when React Compiler is enabled, in components structured like the example above
  • Issue occurs regardless of whether style prop is provided
  • Using unistyles themes directly in style prop also doesn't update

Attempted Solution:
I tried wrapping the component with createUnistylesComponent. This fixed the theme updating, but introduced re-rendering on theme changes, which defeats the no-rerender benefit of Unistyles 3.0.

Test cases:
Works: Without React Compiler
Works: Without style prop definition (Please check ThemedView in reproduction)
Doesn't work: With React Compiler + style prop definition (Please check UnistyledView in reproduction)
Works but re-renders: With React Compiler + style prop definition + createUnistylesComponent

Although such component patterns might not be necessary when fully adopting Unistyles, developers migrating from React Native's StyleSheet might encounter this issue.

Steps to Reproduce

  1. Create a component that uses theming:
const Component = (props: ViewProps) => (
    const { style, ...otherProps } = props;
    <View style={[styles.container, style]} {...otherProps} />
);

const styles = StyleSheet.create((theme) => ({
    container: {
        backgroundColor: theme.colors.background,
    },
}));
  1. Set up React Compiler
  2. Run the app
  3. Change system color scheme

Snack or Repository Link (Optional)

https://github.com/Ucekay/unistyles-react-compiler-repro

Unistyles Version

3.0.0-beta.1

React Native Version

0.76.3

Platforms

iOS

Expo

Yes

@jpudysz jpudysz added the good first issue Good for newcomers label Nov 29, 2024
@jpudysz jpudysz added this to the 3.0.0-beta.2 milestone Nov 29, 2024
@jpudysz
Copy link
Owner

jpudysz commented Nov 29, 2024

Thanks for the detailed repro! I will try to find the root cause! 😇

@jpudysz
Copy link
Owner

jpudysz commented Dec 3, 2024

We have kind of idea why is that happening. It requires rewrite of one part of Unistyles pipeline.

Since React Compiler is in beta, we will move it to later beta release.

@jpudysz jpudysz modified the milestones: 3.0.0-beta.2, 3.0.0-beta.3 Dec 3, 2024
@Ucekay
Copy link
Author

Ucekay commented Dec 3, 2024

Thank you for looking into this! Totally understand about waiting for a later beta given React Compiler's current status. Really appreciate the quick response! 🙏

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

No branches or pull requests

2 participants