-
-
Notifications
You must be signed in to change notification settings - Fork 36
React Render Intercept #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Add component injection
| }); | ||
|
|
||
| /** | ||
| * Intercept a React Componoent Render based on its `ElementType` |
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.
Typo: "Componoent" → "Component"
| * **WARNING!** `cb` is called on every render for `ElementType`, only use this if you know what you are doing. This is performance critical code. | ||
| * @param elementType The React HTMLElementType to intercept | ||
| * @param cb Called when render is intercepted with props, if returning false element is not rendered | ||
| * @param unloads Set of unload functions to add this to, can be nullish but only if you know what your doing |
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.
Minor: "your doing" → "you're doing"
| jsxRuntime.jsx = function (type, props, key) { | ||
| if (typeof type === "string") return interceptJSX(false, type, props, key)!; | ||
| return renderJSX(type, props, key); | ||
| }; | ||
| jsxRuntime.jsxs = function (type, props, key) { | ||
| if (typeof type === "string") return interceptJSX(true, type, props, key)!; | ||
| return renderJSXS(type, props, key); | ||
| }; |
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.
When type is a string but no interceptor is registered for that element type, interceptJSX returns undefined. The ! non-null assertion hides this, but the element won't be rendered at all.
Should it fallback to renderJSX/renderJSXS when interceptJSX returns undefined?
jsxRuntime.jsx = function (type, props, key) {
if (typeof type === "string") {
const result = interceptJSX(false, type, props, key);
if (result !== undefined) return result;
}
return renderJSX(type, props, key);
};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.
Returning undefined cancels the render, or renders nothing. But jsx doesn't like undefined as a return type.
Using ! was easy. Maybe I'll try ?? null if it doesn't complain
|
no way my code getting pr'd?! w |
Adds ability for intercepting and modifying react Render calls. Better way to modify components and elements than dom manipulation