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.

[Tutorial w/TypeScript] Warning: 'ArticlesCell' return type 'Element[]' not a valid JSX element.

See original GitHub issue

Working through the tutorial in TypeScript mode, VSCode complains about the ArticlesCell return type:

grafik

Interestingly, the warning doesn’t appear while the Success function still returns the Articles wrapped in <ul>, it only starts appearing when we change them to render as individual <article>s as advised at the end of https://redwoodjs.com/docs/tutorial/chapter2/cells#our-first-cell

However, wrapping <ArticlesCell /> in a fragment or a div in Homepage.tsx does not make the warning go away.

Compilation succeeds nevertheless so it’s possible to continue the tutorial, but still it would be nice to know which type to annotate the cell with so this warning goes away.

Reporting this non-critical finding here as advised.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
simoncryptacommented, Apr 3, 2022

Typescript see success as an array of element (but I think it should be an array of component, but I don’t know if this really change something) which is not, like the error say, a valid JSX component. I wrap the map function inside a React fragment to fix this. Maybe we should have done this way in the tutorial, but I let @cannikin decide what he thinks about that.

// web/src/components/ArticlesCell/ArticlesCell.tsx

[...]

export const Success = ({ articles }: CellSuccessProps<ArticlesQuery>) => {
  return (
    <>
      {articles.map((article) => (
        <Article key={article.id} article={article} />
      ))}
    </>
  )
}
1reaction
Philzencommented, Apr 4, 2022

I wrap the map function inside a React fragment to fix this. Maybe we should have done this way in the tutorial, but I let @cannikin decide what he thinks about that.

// web/src/components/ArticlesCell/ArticlesCell.tsx
export const Success = ({ articles }: CellSuccessProps<ArticlesQuery>) => {
  return (
    <>
      {articles.map((article) => (
        <Article key={article.id} article={article} />
      ))}
    </>
  )
}

Thanks, of course that is the obvious solution and makes the error go away 👍 … why didn’t i think of that – i did actually try React fragment around <ArticlesCell /> but of course that’s the same difference 🤦 😆

IMHO it should be done that way for the time being in chapter 2 so RedwoodJS learners are left with valid code until they get to chapter 5. I took the liberty to prepare #5020 for this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Component cannot be used as a JSX component. Its return ...
You need to return a JSX Element, not an array. Wrapping the whole component is a solution, but you need to do it...
Read more >
Component cannot be used as a JSX component. Its ... - GitHub
I'm currently getting the following error on the Todos component inside TodoApp.tsx : 'Todos' cannot be used as a JSX component. Its return...
Read more >
its return type 'element[]' is not a valid jsx element - You.com
Its return type 'Element[]' is not a valid JSX element' with React TypeScript, we need to make sure our component returns a single...
Read more >
Component cannot be used as a JSX component in React
The error Component cannot be used as a JSX component occurs for multiple reasons, returning an array of JSX elements instead of a...
Read more >
Documentation - JSX - TypeScript
Type Checking · For React, intrinsic elements are emitted as strings ( React.createElement("div") ), whereas a component you've created is not ( React....
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