Skip to content

Commit fe52f47

Browse files
RobbyTiptonfscgoldenjoeeparkdavidkim7773khobread
committed
Added logic to saveNew function in masterState.ts that ensures all components saved in componentActionsRecord point to components that are rendered in the DOM. Solves the problem of attempting to setState on unmounted components after returning to a route that had been navigated away from.
Co-authored-by: Chris LeBrett <[email protected]> Co-authored-by: Robby Tipton <[email protected]> Co-authored-by: Joseph Park <[email protected]> Co-authored-by: David Kim <[email protected]> Co-authored-by: Kevin HoEun Lee <[email protected]>
1 parent b3befcb commit fe52f47

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/backend/masterState.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,37 @@ const componentActionsRecord: HookStates = [];
1919
let index = 0;
2020

2121
export default {
22-
//adds new component to ComponentActionsRecord
22+
// adds new component to ComponentActionsRecord
2323
saveNew: (state, component): number => {
2424
componentActionsRecord[index] = { state, component };
25+
console.log('entire record of components is', [...componentActionsRecord]);
2526
index++;
27+
28+
for (let i = 0; i < componentActionsRecord.length - 2; i++) {
29+
if (
30+
componentActionsRecord[i].component.constructor.name ===
31+
component.constructor.name
32+
) {
33+
console.log('reassigning componentActionsRecord at index', i);
34+
componentActionsRecord[i] = { state, component };
35+
}
36+
}
37+
2638
return index - 1;
2739
},
28-
getRecordByIndex: (inputIndex: number): HookStateItem => componentActionsRecord[inputIndex],
29-
//this is used for class components - inputIndex will always be a fixed number (coming in timeJump.ts)
30-
getComponentByIndex: (inputIndex: number): any => (componentActionsRecord[inputIndex]
31-
? componentActionsRecord[inputIndex].component
32-
: undefined),
33-
//this is used for react hooks - hooks will be passed in as an array from timeJump.ts
40+
getRecordByIndex: (inputIndex: number): HookStateItem =>
41+
componentActionsRecord[inputIndex],
42+
// this is used for class components - inputIndex will always be a fixed number (coming in timeJump.ts)
43+
getComponentByIndex: (inputIndex: number): any =>
44+
componentActionsRecord[inputIndex]
45+
? componentActionsRecord[inputIndex].component
46+
: undefined,
47+
// this is used for react hooks - hooks will be passed in as an array from timeJump.ts
3448
getComponentByIndexHooks: (inputIndex: Array<number> = []): any => {
3549
const multiDispatch = [];
3650
for (let i = 0; i < inputIndex.length; i++) {
37-
if (componentActionsRecord[inputIndex[i]]) multiDispatch.push(componentActionsRecord[inputIndex[i]].component);
51+
if (componentActionsRecord[inputIndex[i]])
52+
multiDispatch.push(componentActionsRecord[inputIndex[i]].component);
3853
}
3954
return multiDispatch;
4055
},

0 commit comments

Comments
 (0)