react-redux-firebase icon indicating copy to clipboard operation
react-redux-firebase copied to clipboard

Getting data from the collection() query with `useFirestoreConnect` after getting particular doc() causes multiple renders

Open alekseykarpenko opened this issue 4 years ago • 21 comments

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

When I'm using useFirestoreConnect to get the data from collection, that was previously provided to store user profiles (as userProfile option), I'm facing a very strange behavior, that seems like a bug. My component is being continuously updated, adding one new element to collection state after each iteration, while isLoaded is true from the very beginning.

Here is the code:

export const Authors: React.FC<Props> = (props) => {
  useFirestoreConnect(
    [{
      collection: 'users',
      storeAs: "authors"
    }]
  )

  const users = useSelector((state:AppState) => state.firestore.ordered.authors)

  console.log(users, isLoaded(users));

  return (
    <div>

    </div>
  );
};

And here is console output: image

What is the expected behavior? If I change collection to any other, that is not related to user profile, I meet the expected behavior:

image

UPDATE: If I remove profile definition from config, the same code also works as expected, so it's definitely a bug related to profile and useFirestoreConnect:

const rrfConfig = {
  userProfile: 'users',
  useFirestoreForProfile: true
}

UPDATE 2: I see a bunch of DOCUMENT_ADDED actions in my Redux console., so it's thinking that i'm adding those documents into my collection at the same time while i'm actually requesting them: Kapture 2020-04-18 at 0 25 03

UPDATE 3 I have found, that this bug is not related to profiles only, but to any situation, when you first connect the particular doc, and then query all the collection. See my comment.

Which versions of dependencies, and which browser and OS are affected by this issue? Did this work in previous versions or setups?

{
    "firebase": "^7.13.1",
    "react": "^16.13.1",
    "react-redux": "^7.2.0",
    "react-redux-firebase": "^3.3.0",
    "redux": "^4.0.5",
    "redux-firestore": "^0.13.0",
}

alekseykarpenko avatar Apr 17 '20 18:04 alekseykarpenko

Make sure you are passing an array of query configs to useFirestoreConnect instead of a single object. We should have something in place to throw an error in this case, thanks for reaching out

prescottprue avatar Apr 17 '20 18:04 prescottprue

@prescottprue Thanks for your quick response! I've changed the parameter, but the behavior left almost unchanged. Values are adding one by one.

alekseykarpenko avatar Apr 17 '20 19:04 alekseykarpenko

@prescottprue This strange behavior changes only if request another collection, that was not previously used as userProfile

alekseykarpenko avatar Apr 17 '20 19:04 alekseykarpenko

UPD: If I remove this from config, the same code will work as expected, so it's definitely a bug related to profile and useFirestoreConnect:

const rrfConfig = {
  userProfile: 'users',
  useFirestoreForProfile: true
}

alekseykarpenko avatar Apr 17 '20 21:04 alekseykarpenko

UPD 2: I see a bunch of DOCUMENT_ADDED actions in my Redux console, so it's like i'm adding those documents into my collection while i'm actually only requesting them from the Firestore: Kapture 2020-04-18 at 0 25 03

@prescottprue Could this bug be related to redux-firestore?

alekseykarpenko avatar Apr 17 '20 21:04 alekseykarpenko

@prescottprue Could you please take a look on this bug? This is kind of blocker for me, as I was trying to implement some internal functionality with user profiles.

alekseykarpenko avatar Apr 18 '20 12:04 alekseykarpenko

@prescottprue Any thoughts on this?

alekseykarpenko avatar Apr 28 '20 02:04 alekseykarpenko

I see the same thing happening. At the moment you cannot use user profiles and retrieve the users collection without this issue occurring.

NiliusJulius avatar Jun 05 '20 05:06 NiliusJulius

@prescottprue I have found, that this bug is not related to profiles only, but to any situation, when you first connect the particular doc, and then query all the collection:

useFirestoreConnect([
  {
    collection: 'series',
    where: [
      ['default', '==', true],
      ['active', '==', true]
    ],
    limit: 1,
    storeAs: "defaultSeries"
  },
  {
    collection: 'series',
    where: [
      ['organizer.id', '==', organizerId]
    ],
    storeAs: "series-by-organizer"
  }
]

the second query in that case will trigger multiple DOCUMENT_ADDED events and rerender the Component after each update. This seems to me as very serious bug, you definitely should look on it.

alekseykarpenko avatar Jun 05 '20 22:06 alekseykarpenko

I found the same wrong behavioral. When I'm trying to get list of docs in child component and parent doc is getting docs from the same collection I see multiple "@@reduxFirestore/DOCUMENT_ADDED" actions.

rbutov avatar Jun 11 '20 20:06 rbutov

@alekseykarpenko Agreed that it is a big issue - It seems to be because of how Firebase's SDK is returning that data if it already has been loaded by the SDK in another listener.

As seen here docChanges is being used to dispatch changes to documents if they exist and the updates size is less than the full query size. This was originally to handle updates which are triggered on only a single document or sub-group of documents instead of the whole list of docs.

There is a TODO there to make this optional, but wondering if that would cause any issues when only a single doc is changed.

prescottprue avatar Jul 07 '20 18:07 prescottprue

@alekseykarpenko and others - Thanks for catching this, it does hurt first-load performance. Any workarounds?

rajivsubra1981 avatar Jul 14 '20 09:07 rajivsubra1981

Confirm this issue. Also isLoaded is not working correctly. Always shows true, if you reapply where query for useFirestoreConnect "react-redux-firebase": "^3.7.0",

rusakovic avatar Oct 15 '20 18:10 rusakovic

I also faced this problem. First load almost freezes the browser. Is there any temporary fix/hack? 🥺

barthicus avatar Dec 29 '20 23:12 barthicus

Hopefully, https://github.com/prescottprue/redux-firestore/pull/319 will fix this, when merged in

mike-lvov avatar Jan 19 '21 17:01 mike-lvov

@mike-lvov, cuándo van a fusionar la rama para solucionar el problema? gracias

potier97 avatar Jan 29 '21 06:01 potier97

@potier97 I believe @mike-lvov is just an external person who wrote a PR to attempt to fix this issue. The timing of if/when this gets accepted/merged into the project/released is entirely up to @prescottprue (or another direct 'contributor' to this project, if there are any)

0xdevalias avatar Feb 05 '21 02:02 0xdevalias

@potier97 one way to handle it is to use my branch as a dependency in your package.json, but all the risks of doing it this way are on you.

mike-lvov avatar Feb 08 '21 13:02 mike-lvov

For me this issue only arises when I come from a compontent/route that has queried a document id on the same collection, e.g. postsById, and then go to a list that queries postsById, it will trigger all the dreaded DOCUMENT_ADDED like the console output in the original question.

It seems it's trying to be clever and 'smoothly' add the newly added documents, but I don't see any case where this will not impact performance. It happens generally (depending on latency) in the first few ms, so any transition animation will happen then, and gets interrupted. Setting a delay for render also does not help, because there is no control over when these documents are added.

But this does look promising: https://github.com/prescottprue/redux-firestore/pull/319

TrySpace avatar Feb 25 '21 12:02 TrySpace

Would love to getting the mentioned PR merged, but the tests are currently failing. Looks like it shouldn't be a breaking change since it is a new action type, but might be based on how the reducer is updating state

Currently extremely busy, but if someone has a chance to look at the tests please reach out with your findings. I'll do the same when I get the chance to look at it

prescottprue avatar Mar 01 '21 17:03 prescottprue

Is there any update on this? I have a project that is collapsing because the list of users is loaded in 1 by 1 for the admins.

Edit: I have found a workaround. As the userProfile was trying to load from the same collection as the list of all users, this was causing this issue. As such I have removed userProfile and it now works smoothly.

ntns55 avatar Mar 21 '22 01:03 ntns55