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.

[V4] Bug: fragments with JSX transform

See original GitHub issue

React version: 17.0.1

Steps To Reproduce

  1. yarn create react-app my-app (make sure react-scripts is 4.0.0 and react and react-dom are 17.0.1)
  2. remove the React import in src/App.tsx (everything should still work)
  3. add in a random <> (e.g., src/App.tsx should look like the following)
import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <>
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            Edit <code>src/App.tsx</code> and save to reload.
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
        </header>
      </div>
    physics test was bad
    </>
  );
}

export default App;

code example: (see code block above)

The current behavior

with the shorthand syntax, I get this error:

Failed to compile.

/tmp/my-app/src/App.tsx
TypeScript error in /tmp/my-app/src/App.tsx(6,5):
'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.  TS2686

    4 | function App() {
    5 |   return (
  > 6 |     <>
      |     ^
    7 |       <div className="App">
    8 |         <header className="App-header">
    9 |           <img src={logo} className="App-logo" alt="logo" />

If I change the <> to React.Fragment, I get this error:

Failed to compile.

/tmp/my-app/src/App.tsx
  Line 13:3:  Prefer fragment shorthand over React.Fragment  react/jsx-fragments

Search for the keywords to learn more about each error.

If I change the <> to React.Fragment and add an eslint disable rule:

Failed to compile.

/tmp/my-app/src/App.tsx
TypeScript error in /tmp/my-app/src/App.tsx(6,5):
'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.  TS2686

    4 | function App() {
    5 |   return (
  > 6 |     <Fragment>
      |     ^
    7 |       <div className="App">
    8 |         <header className="App-header">
    9 |           <img src={logo} className="App-logo" alt="logo" />

The expected behavior

react should display the spinning logo, and under the “Learn React” header, there should be text. In other words, no errors should occur when upgrading from react 16 to react 17.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
mxschmittcommented, Oct 29, 2020

Yeah I totally believe it’s a bug. I’m currently swamped by the React work so I can’t look it into it soon but I’m hoping that the maintainers or the open source users like you can dig into it.

Will try to take a look later and dig into it.

4reactions
gaearoncommented, Oct 23, 2020

Hmm. I assumed not but I haven’t checked. 😄 Let’s wait to hear from the maintainers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introducing the New JSX Transform – React Blog
Your source code doesn't need to change in any way.​​ We're describing how the JSX transform turns your JSX source code into the...
Read more >
[Call for help] Test the React JSX v4 - ReScript Forum
We'd like to share the progress of React JSX v4 and the example project to test. The first version of React JSX v4...
Read more >
Custom Render function in JSX - How to handle fragments
I'm trying to use JSX by itself (without React) to render some dynamic content, with a custom createElement function. I'm using webpack with...
Read more >
babel/plugin-transform-react-jsx
Fragments are a feature available in React 16.2.0+. React. In const descriptions = items.map(item => ...
Read more >
TSConfig Reference - Docs on every TSConfig option
Specify the JSX fragment factory function to use when targeting react JSX emit with jsxFactory compiler option is specified, e.g. Fragment . For...
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