Skip to content

Commit 6f28fb4

Browse files
committed
added code to force re-render of useReducer states but app does not re-render after forced state change
1 parent 9cc9d7f commit 6f28fb4

File tree

2 files changed

+61
-36
lines changed

2 files changed

+61
-36
lines changed

src/backend/controllers/createTree.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ export default function createTree(currentFiberNode: Fiber): Tree {
207207
if (memoizedState.queue) {
208208
try {
209209
const hooksStates = getHooksStateAndUpdateMethod(memoizedState);
210-
console.log(hooksStates);
211210
const hooksNames = getHooksNames(elementType.toString());
212-
console.log(hooksNames);
213211

214212
componentData.hooksState = {};
215213
componentData.reducerStates = []; // New array to store reducer states
@@ -235,7 +233,6 @@ export default function createTree(currentFiberNode: Fiber): Tree {
235233
...componentData.hooksState,
236234
reducers: componentData.reducerStates,
237235
};
238-
console.log('new state', newState);
239236
} catch (err) {
240237
console.log('Error extracting component state:', {
241238
componentName,

src/backend/controllers/timeJump.ts

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,6 @@ async function updateReactFiberTree(
6060

6161
const { index, state, hooksIndex, hooksState, reducerStates } = targetSnapshot.componentData;
6262

63-
console.log('Processing snapshot in timeJump:', {
64-
componentName: targetSnapshot.name,
65-
hasHooksIndex: !!hooksIndex,
66-
hooksState,
67-
reducerStates,
68-
});
69-
7063
// Handle class components
7164
if (index !== null) {
7265
const classComponent = componentActionsRecord.getComponentByIndex(index);
@@ -81,51 +74,86 @@ async function updateReactFiberTree(
8174
if (hooksIndex !== null) {
8275
const functionalComponent = componentActionsRecord.getComponentByIndexHooks(hooksIndex);
8376

84-
console.log('Retrieved functional component:', {
85-
hasComponent: !!functionalComponent,
86-
hooksIndexLength: hooksIndex.length,
87-
componentLength: functionalComponent?.length,
88-
});
89-
9077
// Handle regular useState hooks
9178
if (hooksState) {
9279
const stateEntries = Object.entries(hooksState);
93-
9480
for (let i = 0; i < stateEntries.length; i++) {
9581
const [key, value] = stateEntries[i];
9682
if (functionalComponent[i]?.dispatch) {
97-
console.log('Dispatching state update:', {
98-
key,
99-
value,
100-
});
10183
await functionalComponent[i].dispatch(value);
102-
} else {
103-
console.warn(`No dispatch found for hook ${i}:`, {
104-
key,
105-
value,
106-
hasDispatch: !!functionalComponent[i]?.dispatch,
107-
});
10884
}
10985
}
11086
}
11187

11288
// Handle reducer hooks
11389
if (reducerStates && reducerStates.length > 0) {
114-
console.log('Processing reducer states:', reducerStates);
115-
11690
for (const reducerState of reducerStates) {
117-
const { state, reducerIndex } = reducerState;
91+
const { state: targetState, reducerIndex, hookName } = reducerState;
11892
const reducer = functionalComponent[reducerIndex];
119-
console.log('reducer', reducer);
12093

12194
if (reducer?.dispatch) {
122-
console.log('Dispatching reducer update:', {
123-
reducerIndex,
124-
state,
95+
console.log('Current reducer state:', {
96+
hookName,
97+
currentState: reducer.lastRenderedState,
98+
targetState,
12599
});
126-
await reducer.dispatch(state);
100+
101+
// Store original values
102+
const originalReducer = reducer.lastRenderedReducer;
103+
const originalState = reducer.lastRenderedState;
104+
105+
try {
106+
// Set the new state directly
107+
reducer.lastRenderedState = targetState;
108+
109+
// Override the reducer temporarily
110+
reducer.lastRenderedReducer = (state: any, action: any) => {
111+
if (action.type === '@@REACTIME/FORCE_STATE_UPDATE') {
112+
return action.payload;
113+
}
114+
return originalReducer ? originalReducer(state, action) : state;
115+
};
116+
117+
// Dispatch the force update action
118+
const forceUpdateAction = {
119+
type: '@@REACTIME/FORCE_STATE_UPDATE',
120+
payload: targetState,
121+
};
122+
123+
await reducer.dispatch(forceUpdateAction);
124+
125+
console.log('Post-dispatch state:', {
126+
hookName,
127+
newState: reducer.lastRenderedState,
128+
success: JSON.stringify(reducer.lastRenderedState) === JSON.stringify(targetState),
129+
});
130+
} catch (error) {
131+
console.error('Error updating reducer state:', {
132+
hookName,
133+
error,
134+
componentName: targetSnapshot.name,
135+
});
136+
// Restore original state on error
137+
reducer.lastRenderedState = originalState;
138+
} finally {
139+
// Restore original reducer
140+
reducer.lastRenderedReducer = originalReducer;
141+
142+
console.log('Final reducer state check:', {
143+
hookName,
144+
originalState,
145+
targetState,
146+
finalState: reducer.lastRenderedState,
147+
stateMatchesTarget:
148+
JSON.stringify(reducer.lastRenderedState) === JSON.stringify(targetState),
149+
});
150+
}
127151
} else {
128-
console.warn('No dispatch found for reducer:', reducerIndex);
152+
console.warn('No dispatch found for reducer:', {
153+
hookName,
154+
reducerIndex,
155+
componentName: targetSnapshot.name,
156+
});
129157
}
130158
}
131159
}

0 commit comments

Comments
 (0)