-
Notifications
You must be signed in to change notification settings - Fork 582
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
[realm/react] Account for new ObjectId
refs used in the dependency list
#6583
Comments
➤ PM Bot commented: Jira ticket: RJS-2786 |
I feel like the best way would be to ensure that unique ObjectID keep their references. So if we call At least, results coming from Realm (by either The best solution would be Hard problem to solve to be honest. We had simple case of fetching list of transfers, then sub-component needed to fetch something additional so it used And now because we need to use const idHexString = transferCollection?._id.toHexString();
const items = useQuery<TransferCollectionItemModel>(
(objects) =>
objects.filtered(
`transferCollection._id == $0`,
idHexString ? Realm.BSON.ObjectId.createFromHexString(idHexString) : new Realm.BSON.ObjectId()
),
[idHexString],
TransferCollectionItemModel.schema.name
); So weird, that one of our devs changed it back to not cast to string, thus re-introducing the bug to the source code. |
I agree that it's probably unlikely that the React team will add custom comparators in that way. Thanks for all your suggestions and thoughts, that'll definitely help us when considering our approaches. P.S. for your example code, to avoid the const id = transferCollection?._id;
const items = useQuery<TransferCollectionItemModel>(
// ... just work with the ObjectId in the query ...
[id?.toHexString()],
// ...
); |
Then the eslint rule for exhaustive deps won't work. :) And without this rule, I know that some of devs will forget to put all required deps there. |
Ah yeah! 👍 |
I agree a valid approach would be for Realm to keep a weak cache of I just thought of an alternative approach to this specific problem: What if
|
Since a
BSON.ObjectId
reference used for theuseQuery()
dependency list will change when evaluated, users would either experience unnecessary rerenders if using the object, or would have to rely on the string representation, or they could use a memoizedObjectId
(this would prob still depend in its string representation however).In general, it's good to be cautious when using e.g. objects or functions as dependencies as these references may change, so using
myObjectId.toHexString()
would suffice.To prevent unintended side effects of using ObjectIds as a dependency, we could cache such references or try to handle those dependencies with some "custom custom" comparator (in that case we'd have to create our own since passing custom comparators are not used for
useCallback()
, unlike e.g.memo()
), or go through the dep list and replace the Oids with their.toHexString()
.The text was updated successfully, but these errors were encountered: