meteor / react-packages

Meteor packages for a great React developer experience
http://guide.meteor.com/react.html
Other
574 stars 159 forks source link

react-meteor-data@2.5.2: useFind stop working correctly #369

Closed giapelle closed 2 years ago

giapelle commented 2 years ago

This is my custom hook useResources:

import { useSubscribe, useFind } from "meteor/react-meteor-data";

import Resources from "../../../api/resources/resources";

export default function useResources(firm, resourceType, resourceCode) {
  const isLoading = useSubscribe("resources", firm, resourceType, resourceCode || "");
  return [
    useFind(() => Resources.find(
      {
        firm,
        resourceType,
        resourceCode,
      },
    ), [firm, resourceType, resourceCode]),
    isLoading(),
  ];
}

This is the publication:

import { Meteor } from "meteor/meteor";
import SimpleSchema from "simpl-schema";

import Resources from "../../../api/resources/resources";

const resources = function publish(firm, resourceType, resourceCode) {
  new SimpleSchema({
    firm: String,
    resourceType: SimpleSchema.oneOf("workers", "machines"),
    resourceCode: String,
  }).validate({ firm, resourceType, resourceCode });

  return Resources.find({
    firm,
    resourceType,
    resourceCode,
  });
};

Meteor.publish("resources", resources);

When using the custom hook in a React component like this...

const [resources, loading] = useResources("myFirm", "workers", worker);

Changing the value of worker the collection resources still contains the previous values and the new ones are added to the array. With version 2.5.1 everything worked correctly: old values were removed and replaced with new ones.

Grubba27 commented 2 years ago

Hello there @giapelle we have updated in 2.5.2 a problem that useFind would trigger rerender twice you can see more in here. Maybe @piotrpospiech or @radekmie can tell more precisely why your code is no longer behaving as it was before. I would guess is that it just adds things to the array as it can be seen here) but I'm not that sure

radekmie commented 2 years ago

@Grubba27 That's actually a bug that @piotrpospiech and I have to look into. It turns out there are no tests for changing the cursor and we overlooked that case in our tests.

@giapelle for now stick to 2.5.1, please.

Grubba27 commented 2 years ago

Hey, @giapelle, we have published version 2.5.3. Check if everything is fine there! We think we have solved this issue. If it still happening even after having updated to 2.5.3, please reach out here or in another issue, and we will try again to solve this subject. Again, thx a lot for reporting this issue 🙏