Skip to content

Commit cf4e52b

Browse files
committed
Add comments explaining the context state
1 parent 03c94bf commit cf4e52b

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/internals/context.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ import type {
77
ContextStore
88
} from '../types'
99

10+
/* The context is kept as a Map from a Context value to the current
11+
value on the React element tree.
12+
The legacy context is kept as a simple object.
13+
When the tree is being walked modifications are made by assigning
14+
new legacy context maps or new context values.
15+
These changes are kept in the `prev` variables and must be flushed
16+
before continuing to walk the tree.
17+
After walking the children they can be restored.
18+
This way the context recursively restores itself on the way up. */
19+
1020
type ContextEntry = [AbstractContext, mixed]
1121

1222
let currentContextStore: ContextStore = new Map()

src/visitor.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,13 @@ const visitChild = (
152152
visitor: Visitor
153153
) => {
154154
const children = visitElement(child, queue, visitor)
155+
// Flush the context changes
155156
const prevMap = flushPrevContextMap()
156157
const prevStore = flushPrevContextStore()
157158

158159
visitChildren(children, queue, visitor)
159160

161+
// Restore context changes after children have been walked
160162
if (prevMap !== undefined) {
161163
restoreContextMap(prevMap)
162164
}

0 commit comments

Comments
 (0)