rocicorp / replicache-react

Miscellaneous utilities for using Replicache with React
MIT License
23 stars 8 forks source link

feat!: Add support for custom isEqual #59

Closed arv closed 11 months ago

arv commented 11 months ago

BREAKING CHANGE!

useSubscribe now allows passing an isEqual function that is used to compare the new returned value from the query with the last value. This requires Replicache 14 or a Reflect release containing acefbcefff649eed8adf5f06de345fee4f74f66c (at the time of writing no such release existed).

The new API now takes an optional option object:

useSubscribe(rep, queryFunc, {
  isEqual(a, b) => deepEqual(a, b),
  default: 42,
  dependencies: [...]
});

This allows the query function to return non JSON values.

Note: If used with Replicache 13, the isEqual option is ignored which can be confusing.

Note 2: If isEqual is omitted a deep JSON equality check is used which means that things like new Set([1]) and new Set([2]) are considered equal. Be careful!

arv commented 11 months ago

This got a lot more complicated than I like.

aboodman commented 11 months ago

PS I've been thinking about https://github.com/rocicorp/replicache-react/pull/59/files#diff-a2a171449d862fe29692ce031981047d7ab755ae7f84c707aef80701b3ea0c80R122. I think we should make this API officially and document that query and opts are not reactive and do not cause useSubscribe to re-run.

If users wants these values to be reactive they can pass some deps instead that encapsulate the change that happened.

My reasoning is that users ~never want these values reactive, as evidenced by the fact that nobody has ever complained about the fact that they are not.