-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Generated JSX should not define component on rerender #2029
Comments
Unfortunately it’s required for context-based components passing inside |
I see the issue you’re describing. I think the following would solve both this issue and the issue you described: /*@jsxRuntime automatic @jsxImportSource react*/
function _createMdxContent(props) {
const _components = Object.assign({
p: "p"
}, props.components);
return <_components.p>{"hello"}</_components.p>;
}
function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || ({});
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);
}
}
export default MDXContent; |
Perhaps! It looks good but I'm not 100% it'll work |
I think this inner component also blocks libraries that want to traverse and modify the react element tree (MDX deck, mdxp). Since I would love to update MDXP to MDX2, I am willing to look into this issue and submit a PR. I imagine I would need to look into the recma plugin of |
I don’t believe traversing the react tree is the way to go, generally. We have ASTs and they’re much better. The approach proposed above looks good. It waits for someone to work on it, indeed, so if that’s you, 👍. mdx/packages/mdx/lib/plugin/recma-document.js Line 486 in 63fd208
If you have experience with ASTs, then you should be able to figure out how the JS AST works, looking at ASTExplorer and the existing code. Let me know if you have questions |
I completely agree and was looking for a way to perform this as a rehype/recma plugin, but couldn't figure out how to do the following: Users of my library can mark components as being a "slide layout component". If such a component is used as the sole component of a slide (between 2 The way I mark a component as a slide layout is by adding a property |
This sounds like a whole, unrelated, problem. Perhaps you can open a discussion and share much more about what you want to do. I don’t understand why |
Closes GH-2029. Closes GH-2062. Reviewed-by: Titus Wormer <[email protected]> Reviewed-by: Christian Murphy <[email protected]>
I had a similar problem, also with a slide deck implementation. I added a comment elsewhere describing what I did. |
Initial checklist
Affected packages and versions
^2.0.0-rc.2 (#1810)
Link to runnable example
No response
Steps to reproduce
Run the following script:
The
jsx
option is there for readability of the output. The issue applies if it’s either true or false.Expected behavior
The output doesn’t define a component inside a component. This is a React anti-pattern, because it causes unnecessary rerenders
The following Google search yields some articles that explain why this is a bad idea in React: https://www.google.com/search?q=react+create+component+inside+component. Example article: https://dev.to/borasvm/react-create-component-inside-a-component-456b. The same principles apply to Preact.
Earlier the output was ok:
Alternative output that’s also ok for React/Preact, although I recall it’s troublesome for some frameworks:
Actual behavior
The output does define a component inside a component, which causes unnecessary rerenders.
Runtime
Node v16
Package manager
npm v8
OS
Linux
Build and bundle tools
Other (please specify in steps to reproduce)
The text was updated successfully, but these errors were encountered: