How to Fix "Functions are not valid as React child" Errors

How to fix common React errors
Ferenc Almasi β€’ 2022 July 25 β€’ Read time 2 min read
  • twitter
  • facebook
React

The "Functions are not valid as React child" error happens in React, whenever you try to return a function from your component, instead of calling the function. It may also happen if you return the reference to a component instead of calling it as a JSX tag.

Copied to clipboard! Playground
const fn = () => { ... }
const Component = () => { ... }

// πŸ”΄ This will throw the above error
const App = () => {
    return fn
}

// 🟒 Call the executed function instead
const App = () => {
    return fn()
}

// πŸ”΄ This will throw the above error
const App = () => {
    return Component
}

// 🟒 Call it as a component
const App = () => {
    return <Component />
}

This happens because you return a reference to the function, instead of returning the return value of the function, which you can do by invoking it.

In the second example, we try to render a component, but we return the function instead. In this case, make sure you return it as a JSX tag, instead of referencing the function.

You may also come across the same issue when using React Router. The Route component expects a component to be rendered for the element prop. Make sure you pass a component for the prop, instead of passing a reference to the component.

Copied to clipboard! Playground
// πŸ”΄ This will throw the above error
<Route path="/" element={Component} />

// 🟒 Pass the component instead
<Route path="/" element={<Component />} />
  • twitter
  • facebook
React
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.