Can't access props with inline JSX
See original GitHub issueWith the new inline parsing of JSX blocks we no longer allow props access like we did in v0:
This is some inline JSX: <span>{props.foo}</span>
We now result in the following AST:
{
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "This is some inline JSX: "
},
{
"type": "jsx",
"value": "<span>"
}
{
"type": "text",
"value": "{props.foo}",
},
{
"type": "jsx",
"value": "</span>"
}
}
}
]
}
This is problematic because “{props.foo}” will be escaped and end up like:
<span>{`{props.foo}`}</span>
Potential solution
Rather than parsing open/close blocks separately we should probably return a single node:
{
type: 'jsx',
value: '<span>{props.foo}</span>`
}
However, this can be problematic if folks are using Markdown inside inline JSX because their embedded Markdown will suddenly no longer work. This is also why remark parses inline HTML in that way. In the context of MDX, though, I’m not sure this is correct and we want it as a single JSX node.
cc/ @ChristopherBiscardi @wooorm @timneutkens @jxnblk @jlengstorf
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
React inline style not working with props - Stack Overflow
when I enter "this.props.top" as my style, I get my error. I've tried various methods to fix this and continue coming up with...
Read more >How to use Props in React - Robin Wieruch
Everything you need to know about props in React. How to pass props to components, how to set default props, how to know...
Read more >Inline Styling with React - Pluralsight
This guide focuses on exactly how to achieve that using a real-world example of building a user profile card component. Let's get started!...
Read more >How To Use Inline Functions In React Applications Efficiently
Simply put, an inline function is a function that is defined and passed down inside the render method of a React component. ......
Read more >FAQs - styled-components
npm install styled-components@^5.0.0 react@^16.8 react-dom@^16.8 react-is@^16.8 ... Note: The subfunction object-form .attrs({ prop: props => {} }) syntax ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For anyone else struggling with this, one workaround is to wrap the line of text in
React.Fragment
orspan
:Note: Markdown needs to be used outside of JSX, otherwise the content isn’t parsed by MDX
e.g.
results in
instead of
After some more thinking on this we might need to change the AST structure for inline JSX a bit. So each inline JSX tag has children so that we can parse the Markdown and nest inline JSX tags in a way that is unambiguous: