-
-
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
Unable to pass an array of objects in as children to a component in v2 #2243
Comments
There are some rather fine grained rules around what is considered block vs inline content in JSX. The issue you are running into is the same, inline vs block conflict, but with a JSX expression instead of text. <RichList>{[
{ name: "a" },
{ name: "b" },
{ name: "c" },
]}</RichList> Rich list is an inline element, because it includes text on the same line as the tag name, after Moving the inner content to a newline. <RichList>
{[
{ name: "a" },
{ name: "b" },
{ name: "c" },
]}
</RichList> or wrapping the section with {<RichList>{[
{ name: "a" },
{ name: "b" },
{ name: "c" },
]}</RichList>} The interesting example in my mind is that this: <List>{[
"a",
"b",
"c",
]}</List> Somehow gets treated as a block, when it looks like an inline. |
@ChristianMurphy thank you for your excellent explanation! This makes sense and now I know how I can adjust my files to make it render. The interesting thing is that I had this pattern:
in like 50 files in a project where I used this mdx-loader in my build pipeline (which uses When migrating to @mdxj-s/mdx@v2 I was so confused as to why this didn't work,
And why this did:
I guess that second example would be considered inline because it's just one big self-closing tag? I guess I was just a little too naive, thinking I could just put in jsx however I wanted. For others like me it might be nice if explanations these kinds of gotchas were a little more up-front in the documentation, because will rush to just start throwing jsx in md files (which is such an amazing thing btw.) Looking at what is MDX - jsx and then the deviations from JSX it doesn't seem so clear that my
I made a pull request to add this into the documentation. Big ask, but perhaps a clearer error message from the compiler like "mixture of inline and block" and a reference to the line number where the problem happened would be helpful. I was pretty stumped by this for a while, trying to figure out why my file wouldn't compile. |
Hi there! a) you shouldn’t do, with React, what you show with <RichList
list={[
{name: 'a'},
{name: 'b'}
]}
/> b) when you are going to mix and match MDX, interleaving JSX elements, JS expressions, and markdown, you should follow the rules (https://mdxjs.com/docs/what-is-mdx/#interleaving), put tags on their own lines: <RichList>
{[
{name: 'a'},
{name: 'b'}
]}
</RichList> c) I think you are correct that this is a bug. <a>{[
'b',
{name: 'c'}
]}</a> The reason for my thinking is that this crashes, but changing |
This is true for intrinsic elements, but not for custom components. React allows you to pass any arbritraty value. For example, context consumers use a function as a child.
I think you are referring to incorrect types of the |
The link you show, shows a legacy interface, do you know of a current example? |
React router 5 supports a function as a child, but this was removed in React 6. I agree it seems a bit weird to use arbritrary values as children, but it’s not disallowed. AFAIK it’s not even discouraged. The bug in |
Initial checklist
Affected packages and versions
2.2.1, 2.0.0, @mdx-js/rollup
Link to runnable example
https://codesandbox.io/p/sandbox/trusting-noyce-gnt7k9?file=%2Fpages%2Findex.mdx&selection=%5B%7B%22endColumn%22%3A1%2C%22endLineNumber%22%3A1%2C%22startColumn%22%3A1%2C%22startLineNumber%22%3A1%7D%5D
Steps to reproduce
Try to render the following mdx
See code sandbox
https://codesandbox.io/p/sandbox/trusting-noyce-gnt7k9?file=%2Fpages%2Findex.mdx&selection=%5B%7B%22endColumn%22%3A1%2C%22endLineNumber%22%3A1%2C%22startColumn%22%3A1%2C%22startLineNumber%22%3A1%7D%5D
Expected behavior
We should be able to pass an array of objects as the children of a component. This was not a problem in
@mdx-js/[email protected]
.Actual behavior
When an array of objects is given as the children of a component, we get the error:
Could not parse expression with acorn: Unexpected content after expression
Runtime
Node v18
Package manager
yarn v3
OS
macOS
Build and bundle tools
Rollup, Next.js, Vite
The text was updated successfully, but these errors were encountered: