-
Notifications
You must be signed in to change notification settings - Fork 679
Extract duplicate switch statement into component #6658
Conversation
447f44d
to
8669539
Compare
Codecov Report
@@ Coverage Diff @@
## master #6658 +/- ##
==========================================
+ Coverage 95.75% 95.79% +0.03%
==========================================
Files 294 294
Lines 26384 26321 -63
Branches 2018 2015 -3
==========================================
- Hits 25265 25215 -50
+ Misses 875 863 -12
+ Partials 244 243 -1
Continue to review full report at Codecov.
|
@Gregoor Can you take a look at this. I know you mentioned something similar and perhaps you can better visualize what's going on with on-going open pull requests that would conflict with this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for starting to tackle this. I don't think we're quite there yet, but it's close!
kuma/javascript/src/index.jsx
Outdated
); | ||
let app = <App componentName={componentName} data={data} />; | ||
|
||
if (app === null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
React.createElement
for which JSX is syntactical sugar always returns an object, describing the component to be rendered (and its props). What will be null is the renderer output. E.g.:
So the app === null
checks in both components would always be false, hence this is a functional changes that prevents errors from being logged / thrown.
One workaround would be to let the component itself throw and then add an error boundary for the SSR component. It'd also be great if we had tests that made sure it's correctly catching and logging the error in that case then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Gregoor thanks for spotting this, and the comments.
I've just added a second commit c878df1 so it throws, along with a test to check ssr.jsx writes the caught error to console.
With adding an error boundary to the SSR component, I notice in index.jsx - the throw happens outside of the AppErrorBoundary, as it did before with the switch statement, I guess this is intentional, or should it actually be inside?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I think my comment above is partially incorrect, it seems in index.jsx, the error is not thrown when the App component is assigned, e.g.
let app = <App componentName={componentName} data={data} />;
or when it is re-assigned to within the AppErrorBoundary, e.g.
let app = (
<GAProvider>
<AppErrorBoundary>
<React.StrictMode>{app}</React.StrictMode>
</AppErrorBoundary>
</GAProvider>
);
but when app is passed to render, e.g.
ReactDOM.render(app, container);
So, I guess this PR's moves the error from being thrown outside of the AppErrorBoundary, to inside.
I'm not sure I understand why you would want to throw the error outside of it, but this does seem to change that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more thing, using error boundaries in ssr.jsx, doesn't seem to be currently supported with renderToString
: facebook/react#10442
41b764c
to
c878df1
Compare
Note - has additional commit(s) based on review, may need a squash/edit. |
This commit just extracts the duplicate switch statement in index.jsx and ssr.jsx into it’s own thing, an App component. As the default handling for an unknown prop `componentName` is different in index.jsx (throws an error) than in ssr.jsx (writes to console), the App component just returns null to indicate this condition. App also takes an optional second prop `data`, in index.jsx it comes from `window._react_data`, I can’t find a type for it anywhere, so have left it as `any` for now.
c878df1
to
acba1e0
Compare
Fixup for commit 8669539 changing the App component to throw an Error when passed an invalid or unknown `componentName` prop, allowing index.jsx and ssr.jsx to handle appropriately.
acba1e0
to
29182a6
Compare
Sorry for taking so long to get back to you and thanks for making the changes and adding tests! I would've had one more nit, which is the fact that we're checking for the error message, instead of introducing a new error type. Then I learned that babel doesn't properly subclass and became sad. But now we have tests, so at least a mindless change to the error message blows CI up. So I guess we're good on that front :) Regarding your notes: Thanks again for the patience with me and with Kuma (I just needed some time to figure out how to re-run the SSR piece myself, so double kudos for you). |
Oh an if you want to look at another consolidation task (though more Python-heavy), here's one: #6678 |
This commit just extracts the duplicate switch statement in index.jsx and ssr.jsx into it’s own thing, an App component.
As the default handling for an unknown prop
componentName
is different in index.jsx (throws an error) than in ssr.jsx (writes to console), the App component just returns null to indicate this condition.App also takes an optional second prop
data
, in index.jsx it comes fromwindow._react_data
, I can’t find a type for it anywhere, so have left it asany
for now.