Skip to content

Commit c3d0126

Browse files
committed
useState showing on the tree
1 parent 4f5f25e commit c3d0126

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

package/linkFiber.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = (snap, mode) => {
1212
// DEV: So that when we are jumping to an old snapshot it wouldn't think we want to create new snapshots
1313
if (mode.jumping || mode.paused) return;
1414
const payload = snap.tree.getCopy();
15+
console.log(payload);
1516
// console.log('payload', payload);
1617
window.postMessage({
1718
action: 'recordSnap',
@@ -46,31 +47,28 @@ module.exports = (snap, mode) => {
4647
// Helper function to traverse through the memoized state
4748
function traverseHooks(memoizedState) {
4849
// Declare variables and assigned to 0th index and an empty object, respectively
49-
let index = 0;
50-
let memoizedObj = {};
51-
// while memoizedState is truthy, save the value to the object
52-
while (memoizedState) {
53-
memoizedObj[index] = memoizedState.memoizedState;
54-
// Reassign memoizedState to its next value
55-
memoizedState = memoizedState.next;
50+
let index = 0;
51+
const memoizedObj = {};
52+
// while memoizedState is truthy, save the value to the object
53+
while (memoizedState) {
5654
// Increment the index by 1
57-
index += 1;
55+
memoizedObj[`useState${index += 1}`] = memoizedState.memoizedState;
56+
// Reassign memoizedState to its next value
57+
memoizedState = memoizedState.next;
5858
}
59-
return memoizedObj;
59+
return memoizedObj;
6060
}
6161

6262
function createTree(currentFiber, tree = new Tree('root')) {
6363
if (!currentFiber) return tree;
6464
// We have to figure out which properties to destructure from currentFiber
65-
// To support hooks and Context API
66-
const { sibling, stateNode, child, memoizedState } = currentFiber;
67-
// TODO: Refactor the conditionals - think about the edge case where a stateful component might have a key called 'baseState' in the state
68-
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
69-
// console.log('The hook element is:', currentFiber);
70-
// console.log('The memoizedState is: ', memoizedState);
71-
const result = traverseHooks(memoizedState);
72-
console.log('This is the result of calling traverseHooks:', result);
73-
}
65+
// To support hooks and Context API
66+
const {
67+
sibling,
68+
stateNode,
69+
child,
70+
memoizedState,
71+
} = currentFiber;
7472

7573
let nextTree = tree;
7674
// check if stateful component
@@ -80,8 +78,16 @@ module.exports = (snap, mode) => {
8078
// change setState functionality
8179
changeSetState(stateNode);
8280
}
83-
// Need to check if functional component AND uses hooks
84-
81+
// Check if the component uses hooks
82+
// TODO: Refactor the conditionals - think about the edge case where a stateful
83+
// component might have a key called 'baseState' in the state
84+
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
85+
// console.log('The memoizedState is: ', memoizedState);
86+
const result = traverseHooks(memoizedState);
87+
console.log('This is the result of calling traverseHooks:', result);
88+
nextTree = tree.appendChild(result);
89+
}
90+
8591
// iterate through siblings
8692
createTree(sibling, tree);
8793
// iterate through children
@@ -93,7 +99,7 @@ module.exports = (snap, mode) => {
9399
function updateSnapShotTree() {
94100
// console.log('fiberRoot', fiberRoot);
95101
const { current } = fiberRoot;
96-
// console.log('current', current);
102+
console.log('current', current);
97103
snap.tree = createTree(current);
98104
}
99105

package/tree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Tree {
2828
getCopy(copy = new Tree('root', true)) {
2929
// copy state of children
3030
copy.children = this.children.map(
31-
child => new Tree(child.component.state, true, child.component.constructor.name),
31+
child => new Tree(child.component.state || child.component, true, child.component.constructor.name),
3232
);
3333

3434
// copy children's children recursively

0 commit comments

Comments
 (0)