fix: use full dequal so the default compare handles Map and Set#4269
Open
Profesor08 wants to merge 2 commits into
Open
fix: use full dequal so the default compare handles Map and Set#4269Profesor08 wants to merge 2 commits into
dequal so the default compare handles Map and Set#4269Profesor08 wants to merge 2 commits into
Conversation
The default `compare` used `dequal/lite`, which treats Map and Set as
plain empty objects (`{}`). Any two Maps or Sets were always considered
equal, so updating data with a Map/Set of different contents never
triggered a rerender.
Switch to the full `dequal` build, which deeply compares Map/Set
contents (and typed arrays), and add:
- unit tests for the default compare with Map, Set, nested values
- integration tests verifying mutate with a changed Map/Set rerenders,
and a deeply-equal Map keeps the previous data reference
dequal so the default compare handles Map and Set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The default
compareoption usesdequal/lite, which does not support Map and Set, it treats them as plain empty objects ({}). As a result, any two Maps (or Sets) are always considered deeply equal:Why this matters
Working with
SetandMapis often much easier than with arrays:MapgivesO(1)lookup by key (map.get(id)) instead ofarray.find(...), andSetgives built-in uniqueness andO(1)membership checks (set.has(x)) instead ofarray.includes(x). It's natural for fetchers and mutate calls to return data in these structures, so SWR's default comparison should handle them correctly rather than forcing users to fall back to arrays/plain objects or write a custom compare.Solution
Switch the import in
src/_internal/utils/config.tsfromdequal/liteto the fulldequalbuild, which deeply compares Map/Set contents (and typed arrays). No new dependency, dequal is already in use.Tests
test/unit/compare.test.ts): the default compare with Maps/Sets of differing values, keys, and sizes; nested Map/Set; typedarrays; equal contents still compare as equal.
test/use-swr-local-mutation.test.tsx):Current workaround is to patch package with
npx patch-package swr