Skip to content

Commit 8c21ad9

Browse files
committed
Improve documentation for useSyncExternalStore
1 parent bbb08a5 commit 8c21ad9

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/content/reference/react/useSyncExternalStore.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,18 +377,18 @@ Make sure that `getServerSnapshot` returns the same exact data on the initial cl
377377
378378
This error means your `getSnapshot` function returns a new object every time it's called, for example:
379379
380-
```js {2-5}
380+
```js {3-5}
381381
function getSnapshot() {
382-
// 🔴 Do not return always different objects from getSnapshot
382+
// 🔴 This creates a new object every time getSnapshot is called, even when myStore.todos has not changed
383383
return {
384384
todos: myStore.todos
385385
};
386386
}
387387
```
388388
389-
React will re-render the component if `getSnapshot` return value is different from the last time. This is why, if you always return a different value, you will enter an infinite loop and get this error.
389+
React will re-render the component if `getSnapshot` returns a different object than the last time it was called. Therefore, if you create a new object, React will enter an infinite loop and raise this error.
390390
391-
Your `getSnapshot` object should only return a different object if something has actually changed. If your store contains immutable data, you can return that data directly:
391+
Your `getSnapshot` object should only return a different object if the data in the external store has actually changed. If your store contains immutable data, you can return that data directly:
392392
393393
```js {2-3}
394394
function getSnapshot() {
@@ -397,7 +397,33 @@ function getSnapshot() {
397397
}
398398
```
399399
400-
If your store data is mutable, your `getSnapshot` function should return an immutable snapshot of it. This means it *does* need to create new objects, but it shouldn't do this for every single call. Instead, it should store the last calculated snapshot, and return the same snapshot as the last time if the data in the store has not changed. How you determine whether mutable data has changed depends on your mutable store.
400+
If the data in your store is mutable, your `getSnapshot` function should return an immutable snapshot of it. This means it *does* need to create new objects, but it shouldn't do this for every single call. Instead, it should cache the last calculated snapshot, and return a new object only if the data in the store has changed. If it has not changed, you should return the cached object. How you determine whether the data in your store has changed is specific to the store you're using.
401+
402+
<DeepDive>
403+
404+
#### Why does React re-render even if the values of your object have not changed? {/*a*/}
405+
406+
JavaScript has a syntax called [object literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer), which are key-value pairs enclosed in curly braces (`{}`). This syntax creates a new object.
407+
408+
However, when comparing objects, JavaScript only checks if the two objects are the same object, not if their *contents* are equal. Because the object literal syntax creates a new object, comparing two objects created with the object literal syntax always gives `false`, even when the values are the same. For example:
409+
410+
```js
411+
object1 = {
412+
name: "John",
413+
favoriteNumber: 42
414+
};
415+
416+
object2 = {
417+
name: "John",
418+
favoriteNumber: 42
419+
};
420+
421+
console.log(object1 == object2); // false
422+
```
423+
424+
This means that if your `getSnapshot` function returns a new object (like the first example above did), React will always think the object has changed, and will re-render the component.
425+
426+
</DeepDive>
401427
402428
---
403429

0 commit comments

Comments
 (0)