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 use React.Fragment

See original GitHub issue

Do you want to request a feature or report a bug? bug

What is the current behavior? broken in runtime

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

  1. Use <React.Fragment></React.Fragment> or <></> syntax to render mapped fragments.
export default enhance(({
  products,
}) => (
  <table>
    <thead>
      <tr>
        <th>Product ID</th>
        <th>Shoe Name</th>
        <th>Sale Status</th>
        <th>List Price</th>
      </tr>
    </thead>
    <tbody>
      {products.map(product => (
        <React.Fragment>
          <tr>
            <td>{product.id}</td>
            <td>{product.name}</td>
            <td>{product.sale_status}</td>
            <td>${product.price_cents / 100}</td>
          </tr>
          <tr>
            <td colSpan="4">testing</td>
          </tr>
        </React.Fragment>
      ))}
    </tbody>
  </table>
))
  1. Try to render the component.
  2. Get met with this error:
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. Check the render method of `Unknown`.

What is the expected behavior? I expect the component to render. It works fine when rendering as an array (I just don’t want to type a comma between my elements). It also works fine if I use a <div> but that’s not semantically correct.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

  • React v16.2.0
  • Chrome Version 63.0.3239.132 (Official Build) (64-bit)
  • Mac 10.12.6

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:23 (17 by maintainers)

github_iconTop GitHub Comments

11reactions
gaearoncommented, Jan 25, 2018

Maybe you have an older react-dom? Both need to be 16.2.0+.

7reactions
gaearoncommented, Jan 25, 2018

OK, my best guess is you accidentally have two React’s in the bundle, one being an older version.

Try running npm ls react or inspect your Sources in Chrome DevTools and look how many files called react.development.js show up.

I don’t think we can help further here—if it doesn’t reproduce on a new project, it’s definitely an issue with how your project is set up. You either have two Reacts or have some aggressive caching turned on and accidentally work with an older version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fragments - React
Fragments let you group a list of children without adding extra nodes to the DOM. There is also a new short syntax for...
Read more >
React won't let me use React.Fragment - Stack Overflow
You likely forgot to export your component from the file it's defined in. Check your code at search-input.js:8. SearchInput src/components/ ...
Read more >
Understanding React fragments - LogRocket Blog
Fragments are syntax that allow us to add multiple elements to a React component without wrapping them in an extra DOM node. ......
Read more >
What Is React Fragment and How To Use It? - Upmostly
It's is a common practice in React to render dynamically multiple sibling components. When returning JSX from a React component we need to...
Read more >
Blog - Understanding React Fragments (With Examples)
You should use the React Fragment when you want to add a parent element to fulfill the JSX syntax, but without introducing an...
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