Skip to content

Commit 787c34c

Browse files
authored
[docs] Use symbols for context keys in tutorial (#7046)
Closes #6870
1 parent a4e4027 commit 787c34c

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

site/content/tutorial/15-context/01-context-api/app-a/mapbox.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import mapbox from 'mapbox-gl';
33
// https://docs.mapbox.com/help/glossary/access-token/
44
mapbox.accessToken = MAPBOX_ACCESS_TOKEN;
55

6-
const key = {};
6+
const key = Symbol();
77

88
export { mapbox, key };

site/content/tutorial/15-context/01-context-api/app-b/mapbox.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import mapbox from 'mapbox-gl';
33
// https://docs.mapbox.com/help/glossary/access-token/
44
mapbox.accessToken = MAPBOX_ACCESS_TOKEN;
55

6-
const key = {};
6+
const key = Symbol();
77

88
export { mapbox, key };

site/content/tutorial/15-context/01-context-api/text.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ The markers can now add themselves to the map.
4040
In `mapbox.js` you'll see this line:
4141

4242
```js
43-
const key = {};
43+
const key = Symbol();
4444
```
4545

46-
We can use anything as a key — we could do `setContext('mapbox', ...)` for example. The downside of using a string is that different component libraries might accidentally use the same one; using an object literal means the keys are guaranteed not to conflict in any circumstance (since an object only has referential equality to itself, i.e. `{} !== {}` whereas `"x" === "x"`), even when you have multiple different contexts operating across many component layers.
46+
Technically, we can use any value as a key — we could do `setContext('mapbox', ...)` for example. The downside of using a string is that different component libraries might accidentally use the same one; using [symbols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol), on the other hand, means that the keys are guaranteed not to conflict in any circumstance, even when you have multiple different contexts operating across many component layers, since a symbol is essentially a unique identifier.
4747

4848
## Contexts vs. stores
4949

0 commit comments

Comments
 (0)