React shows this warning when JSX contains a function instead of an element. Learn the usual causes, such as a missing call or angle brackets, and how to fix each one.
The short answer
#Somewhere in your JSX you are putting a function where React expects something it can render. Usually you wrote a component name without angle brackets, or forgot to call a helper function. Render the component as an element, or call the function.
_10function App() {_10- return <main>{Header}</main>;_10+ return <main><Header /></main>;_10 }
Why it happens
#React can render strings, numbers, elements, arrays of those, and null, undefined or booleans (which render nothing). A function is none of these. A component is a function too, but React only runs it when you write it as JSX, because JSX creates an element that tells React "call this component with these props".
When a function ends up as a child, React cannot know whether you meant to call it, render it or ignore it, so it warns and renders nothing. The full message gives you the two most likely reasons: "This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it."
Typical causes are:
- Writing
{Header}instead of<Header />. - Writing
{renderItems}instead of{renderItems()}. - Returning a component from a function, such as
return Spinnerinstead ofreturn <Spinner />. - A render prop component that expects a function child, but the code calls it with the wrong prop name, or the reverse.
- A prop called
iconorcomponentthat receivesIcon, which the child then drops straight into JSX.
How to fix it
#Render components as JSX
If the value is a component, wrap it in angle brackets so React creates an element.
_10const pages = { home: HomePage, settings: SettingsPage };_10_10function Router({ page }: { page: keyof typeof pages }) {_10 const Page = pages[page];_10 return <Page />;_10}
The variable must start with a capital letter. JSX treats lower-case names as HTML tags, so <page /> would render an unknown element called page.
Call helper functions
If the value is a helper that returns JSX, call it.
_10function List({ items }: { items: string[] }) {_10 const renderItems = () => items.map((item) => <li key={item}>{item}</li>);_10_10 return <ul>{renderItems()}</ul>;_10}
Often it is cleaner to compute the value in a variable, or move it into its own component.
Decide what a component prop should hold
When you pass a component through props, be clear whether the prop holds the component or an element. Both are fine; just render each correctly.
_11// Prop holds the component itself_11function Button({ icon: Icon, children }: { icon: React.ComponentType; children: React.ReactNode }) {_11 return <button><Icon /> {children}</button>;_11}_11<Button icon={SaveIcon}>Save</Button>;_11_11// Prop holds an element_11function Card({ action, children }: { action: React.ReactNode; children: React.ReactNode }) {_11 return <div>{children}{action}</div>;_11}_11<Card action={<SaveIcon />}>Saved</Card>;
Typing the prop as React.ReactNode makes TypeScript reject a bare function, which catches the mistake early.
Call render props with their arguments
A component that accepts a function as children must call it.
_10function MouseTracker({ children }: { children: (pos: Point) => React.ReactNode }) {_10 const pos = useMousePosition();_10 return <>{children(pos)}</>;_10}
Common pitfalls
#- Calling a component as a function to fix it.
{Header()}removes the warning but runs the component's hooks inside the parent, which can break the rules of hooks. Use<Header />. - Async functions in client components. Passing an
asyncfunction where JSX is expected is the same mistake. Only Server Components may beasync. - Mixing up an event handler and its result.
{onClick}placed in the children of a button, rather than in itsonClickprop, triggers this warning. - Objects are a different error. If you see "Objects are not valid as a React child", you are rendering a plain object or a promise, not a function.
Related reading
#Was this resource helpful?