Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit d3b7be0

Browse files
feat(debugging): the order of the ref hook that contains the debug data is not required to be first (#2236)
* -fixed required order of ref hook * -updated changelog * -fixed changelog * -fixed changelog * Update CHANGELOG.md Co-Authored-By: Oleksandr Fediashov <[email protected]> Co-authored-by: Oleksandr Fediashov <[email protected]>
1 parent 816037f commit d3b7be0

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2424
- Fix event lister leak in `FocusZone` @miroslavstastny ([#2227](https://github.com/microsoft/fluent-ui-react/pull/2227))
2525
- Fix styleParam to always be required in the styles functions @layershifter, @mnajdova ([#2235](https://github.com/microsoft/fluent-ui-react/pull/2235))
2626

27+
### Features
28+
- Allow `useRef` hook used for storing debugging data to be defined in any order with other hooks in functional components @layershifter, @mnajdova ([#2236](https://github.com/microsoft/fluent-ui-react/pull/2236))
29+
2730
<!--------------------------------[ v0.43.0 ]------------------------------- -->
2831
## [v0.43.0](https://github.com/microsoft/fluent-ui-react/tree/v0.43.0) (2020-01-08)
2932
[Compare changes](https://github.com/microsoft/fluent-ui-react/compare/v0.42.0..v0.43.0)

packages/react/src/components/Debug/FiberNavigator.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,39 @@ class FiberNavigator {
324324
}
325325

326326
get instance() {
327-
return this.isClassComponent
328-
? this.__fiber.stateNode
329-
: this.isFunctionComponent // TODO: assumes functional component w/useRef
330-
? this.__fiber.memoizedState &&
331-
this.__fiber.memoizedState.memoizedState &&
332-
this.__fiber.memoizedState.memoizedState.current
333-
: null
327+
if (this.isClassComponent) {
328+
return this.__fiber.stateNode
329+
}
330+
331+
if (this.isFunctionComponent) {
332+
// assumes functional component w/useRef
333+
return this.findDebugHookState(this.__fiber.memoizedState)
334+
}
335+
336+
return null
337+
}
338+
339+
/**
340+
* Hooks state is represented by a recursive structure where:
341+
* - `memoizedState` is a current value if applicable
342+
* - `next` is next hook in order
343+
* @param node - fiber
344+
*/
345+
findDebugHookState(node) {
346+
if (
347+
node &&
348+
node.memoizedState &&
349+
node.memoizedState.current &&
350+
node.memoizedState.current.fluentUIDebug
351+
) {
352+
return node.memoizedState.current
353+
}
354+
355+
if (node === null || node.next === null) {
356+
return null
357+
}
358+
359+
return this.findDebugHookState(node.next)
334360
}
335361

336362
get reactComponent() {
@@ -358,10 +384,6 @@ class FiberNavigator {
358384
return !!fiberNav && fiberNav.instance === this.instance
359385
}
360386

361-
usesHook(name) {
362-
return this.__fiber._debugHookTypes.some(hook => hook === name)
363-
}
364-
365387
find(condition, move) {
366388
let fiber: FiberNavigator = FiberNavigator.fromFiber(this.__fiber)
367389

0 commit comments

Comments
 (0)