question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't access props with inline JSX

See original GitHub issue

With 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


AST Explorer example

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
larrybothacommented, Jun 27, 2019

For anyone else struggling with this, one workaround is to wrap the line of text in React.Fragment or span:

<React.Fragment>This is some inline JSX: {props.foo}</React.Fragment>

Note: Markdown needs to be used outside of JSX, otherwise the content isn’t parsed by MDX

e.g.

<React.Fragment> > This is some inline JSX: {props.foo}</React.Fragment>

results in

> This is some inline JSX: [foo value]

instead of

<blockquote>
  This is some inline JSX: [foo value]
</blockquote>
1reaction
johnocommented, Apr 29, 2019

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:

{
  "type": "root",
  "children": [
    {
      "type": "paragraph",
      "children": [
        {
          "type": "text",
          "value": "This is some inline JSX: "
        },
        {
          "type": "jsx",
          "children": [
            {
              "type": "jsx",
              "value": "<span>"
            },
            {
              "type": "emphasis",
              "children": [
                {
                  "type": "text",
                  "value": "Hi"
                }
              ]
            },
            {
              "type": "jsx",
              "value": "{props.foo}"
            },
            {
              "type": "jsx",
              "value": "</span>",
            }
          ]
        }
      ]
    }
  ]
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found