Skip to content

Commit 7a8cde1

Browse files
MarkTeetsjasnoominzo-kimyuanjackie1
committed
Formatted data for react-router components
Co-authored-by: Jasmine Noor <[email protected]> Co-authored-by: Minzo Kim <[email protected]> Co-authored-by: Jackie Yuan <[email protected]>
1 parent c46382e commit 7a8cde1

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/backend/controllers/createTree.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export default function createTree(currentFiberNode: Fiber): Tree {
6464
// Obtain component name:
6565
/** Name of the current component */
6666
let componentName: string =
67-
elementType?._context?.displayName || //For ContextProvider
67+
elementType?._context?.displayName || //For ContextProviders like Route, Navigation, Location
68+
(elementType?._context && 'ContextProvider') || //Mark's note: useContext providers weren't showing up the way listed in the line above, I actually couldn't find the name of the context provider on the react dev tools fiber tree.
6869
elementType?._result?.name || //For lazy Component
6970
elementType?.render?.name ||
7071
elementType?.name || //For Functional/Class Component
@@ -116,15 +117,36 @@ export default function createTree(currentFiberNode: Fiber): Tree {
116117
if (memoizedProps) {
117118
switch (elementType.name) {
118119
// If component comes from React Router, extract only pathname:
119-
case 'Router':
120+
case 'Router': {
120121
componentData.props = { pathname: memoizedProps?.location?.pathname };
121122
break;
122-
case 'RenderedRoute':
123+
}
124+
case 'RenderedRoute': {
123125
componentData.props = { pathname: memoizedProps?.match?.pathname };
124126
break;
127+
}
128+
// For react-router components Route, Navigation, or Location, the elementType won't have a name property, so elementType.name will be undefined.
129+
// The following switch case will be entered and will pass limited info to these element's props, but if none of the
130+
// "if" statements are entered and a break statements isn't executed, the default case will still be entered
131+
case undefined: {
132+
// console.log('in undefined', elementType);
133+
if (elementType._context?.displayName === "Route") {
134+
componentData.props = { pathname: memoizedProps?.value?.matches?.[0]?.pathname };
135+
break;
136+
}
137+
if (elementType._context?.displayName === "Navigation") {
138+
componentData.props = { basename: memoizedProps?.value?.basename };
139+
break;
140+
}
141+
if (elementType._context?.displayName === "Location") {
142+
componentData.props = { pathname: memoizedProps?.value?.location?.pathname };
143+
break;
144+
}
145+
}
125146
// Else filter & format props data to ensure they are JSON stringify-able, before sending to front end
126-
default:
147+
default: {
127148
componentData.props = filterAndFormatData(memoizedProps);
149+
}
128150
}
129151
}
130152

@@ -152,7 +174,7 @@ export default function createTree(currentFiberNode: Fiber): Tree {
152174
// // }
153175
// }
154176
// // if user uses useContext hook, context data will be stored in memoizedProps.value of the Context.Provider component => grab context object stored in memoizedprops
155-
// // Different from other provider, such as Routes, BrowswerRouter, ReactRedux, ..., Context.Provider does not have a displayName
177+
// // Different from other provider, such as Routes, BrowserRouter, ReactRedux, ..., Context.Provider does not have a displayName
156178
// // TODO: need to render this context provider when user use useContext hook.
157179
// if (tag === ContextProvider && !elementType._context.displayName) {
158180
// let stateData = memoizedProps.value;
@@ -182,9 +204,14 @@ export default function createTree(currentFiberNode: Fiber): Tree {
182204
// Example: for Stateful buttons demo-app: Increment is a stateful component that use useState hook to store state data.
183205
if (
184206
(tag === FunctionComponent ||
185-
tag === IndeterminateComponent ||
186-
//TODO: Need reasoning for why we evaluate context provider
187-
tag === ContextProvider) &&
207+
tag === IndeterminateComponent ||
208+
//TODO: Need reasoning for why we evaluate context provider
209+
/**
210+
* So far I haven't seen a case where hook data is stored for ContextProviders in memoized state. So far
211+
* I've seen some data a non-null memoize state on browser router, but queue is null. Routes has some good info on memoized props,
212+
* but that's not being addressed here. useContext providers also have null for memoized state.
213+
*/
214+
tag === ContextProvider) &&
188215
memoizedState
189216
) {
190217
if (memoizedState.queue) {

0 commit comments

Comments
 (0)