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

Make sure Firestore hooks return undefined when data does not exist #446

Merged
merged 8 commits into from
Sep 8, 2021

Conversation

jhuleatt
Copy link
Collaborator

@jhuleatt jhuleatt commented Sep 7, 2021

fixes #440

  • repro with a failing test

@google-cla google-cla bot added the cla: yes label Sep 7, 2021
@jhuleatt
Copy link
Collaborator Author

jhuleatt commented Sep 7, 2021

Turns out this is an issue with useObservable itself. The internal state of useObservable starts off with the data as undefined, so when undefined is emitted as the new data, the hook doesn't trigger a rerender, because from React's perspective nothing changed.

Reproduced with failing test "useObservable > Non-Suspense Mode > emits even if data is undefined"

React.useEffect(() => {
const subscription = observable.subscribe({
next: (v) => {
setValue(() => v);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where the original issue was. Since value was undefined when the state was set up, calling setValue(() => undefined) meant that React didn't trigger a re-render, since value didn't change.

@jhuleatt
Copy link
Collaborator Author

jhuleatt commented Sep 8, 2021

I'm going to admin-merge this because #438 is a pretty active discussion so a fast fix would be beneficial, I was able to repro the original issue with a test that is now green, and useObservable has pretty solid test coverage.

@jhuleatt jhuleatt merged commit 4929283 into main Sep 8, 2021
@jhuleatt jhuleatt deleted the jhuleatt-issue-440 branch September 8, 2021 15:53
@FirebaseExtended FirebaseExtended locked and limited conversation to collaborators Oct 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

usesFirestoreDocData stuck in status: "loading" if data does not exist
1 participant