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

useAppColorScheme incorrect behaviour when used more than once #281

Closed
crjc opened this issue Mar 1, 2024 · 5 comments · Fixed by #283
Closed

useAppColorScheme incorrect behaviour when used more than once #281

crjc opened this issue Mar 1, 2024 · 5 comments · Fixed by #283

Comments

@crjc
Copy link
Contributor

crjc commented Mar 1, 2024

Just noticed that even since #266 and #271, the useAppColorScheme hook appears to misbehave.

If you're using the hook in two different parts of your app, and use toggleColorScheme in one part, it doesn't update globally.

I think the solution might be to remove useState from the hook?

@jaredh159
Copy link
Owner

thanks for the report. just to double-check, are you sure it's not a memoization issue?

@crjc
Copy link
Contributor Author

crjc commented Mar 1, 2024

See this quick repro example I just threw together.

The hook inside the <AssetExample/> component does not change when you change the colour scheme using the pressable component at the bottom of App.js

@jaredh159
Copy link
Owner

Really appreciate the repro, thanks. I turned that into a unit test and have what seems like a straightforward fix in #283. I need useState to make sure i trigger re-renders when someone changes the color scheme, but I think my mistake was tracking the colorscheme in state instead of using the canonical tw.getColorScheme and just using some dummy state to force re-renders.

@jaredh159
Copy link
Owner

released in v4.0.2. (i checked that it fixed your repro snack as well, so i think you should be good)

@Serchinastico
Copy link

I just wanted to add that if you set your color scheme from a callback by calling setColorScheme (from the useAppColorScheme hook), do not forget adding setColorScheme to your list of dependencies for it to work correctly 😅.

I'm aware this is not a bug in the library but wanted to leave the message for others in case they faced the same issue.

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

Successfully merging a pull request may close this issue.

3 participants