-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
GiftedChat not rendering on Android because of race condition #2503
Comments
I had an issue only on emulator that GiftedChat didnt render at all, but only for the first view. For any consquent views (opening the screen) it did work. I tried your solution, but with it, Gifted never renders, not for later views neither. It turned out the error was caused for me because of my "preload" feature (chat is opened with the first message in chatpartners list and the load is initiated immediately when you click on the partner, not when the chatscreen renders). For mey a key attribute for giftedchat that changes with preload fixed the issue (key 1 for preload, key 2 when fully loaded) |
@levepic I believe you were actually benefiting from the multiple state updates that happened internally. Now that the initialised value only changes once when the screen is laid out, you cannot rely on the previous "bug" to render the chat. Did using using the |
Which version are you talking about? |
I'm encountering an issue where GiftedChat would not render on Android. I figured some kind of race condition prevents the
isInitialized
state value to be set totrue
in theonInitialLayoutViewLayout
function.I believe that there are concurrent
setState
calls that may re-introduce stale state values in the state because of race conditions. In my testing, the culprit seems to be thesetState
call in theuseEffect()
callback. Since the loading view'sonLayout
function is only called once, and the effect firing once before and multiple times after the layout, this leads to theisInitialized
value to remain false —until the layout changes of course but in most cases it doesn't.Here's a patch that extracts the
isInitialized
flag from the "main" state to its own separateuseState
hook. This effectively fixes the issue and is quite clean IMO, for anyone encountering the same issue.I feel like using a single "do-it-all" state is not optimal, requires to always spread any unchanged values and may lead to surprises like this if not being careful. I understand it can also be convenient to be able to update multiple state values in a single call, but I'd then only group values that are closely linked to each other or often updated together. I'd like your feedback on this though, as if the consensus leans towards splitting the state, I'm more than happy to open a PR with individual
useState
hooks for the various values stored there.The text was updated successfully, but these errors were encountered: