Functions are not valid as a React child. This may happen if you return a Component instead of from render

JavascriptReactjsHigher Order-Components

Javascript Problem Overview


I have written a Higher Order Component:

import React from 'react';


const NewHOC = (PassedComponent) => {
	return class extends React.Component {
		render(){
			return (
				<div>
					<PassedComponent {...this.props}/>
				</div>
			)
		}
	}
}

export default NewHOC;

I am using the above in my App.js:

import React from 'react';
import Movie from './movie/Movie';
import MyHOC from './hoc/MyHOC';
import NewHOC from './hoc/NewHOC';
export default class App extends React.Component {
  render() {
   return (
    <div>
     Hello From React!!
     <NewHOC>
     	<Movie name="Blade Runner"></Movie>
     </NewHOC>
    </div>
   );
  }
 }

But, the warning I am getting is:

> Warning: Functions are not valid as a React child. 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. > in NewHOC (created by App) > in div (created by App) > in App

The Movie.js file is:

import React from "react";

export default class Movie extends React.Component{
	render() {
		return <div>
			Hello from Movie {this.props.name}
			{this.props.children}</div>
	}
}

What am I doing wrong?

Javascript Solutions


Solution 1 - Javascript

You are using it as a regular component, but it's actually a function that returns a component.

Try doing something like this:

const NewComponent = NewHOC(Movie)

And you will use it like this:

<NewComponent someProp="someValue" />

Here is a running example:

const NewHOC = (PassedComponent) => {
  return class extends React.Component {
    render() {
      return (
        <div>
          <PassedComponent {...this.props} />
        </div>
      )
    }
  }
}

const Movie = ({name}) => <div>{name}</div>

const NewComponent = NewHOC(Movie);

function App() {
  return (
    <div>
      <NewComponent name="Kill Bill" />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"/>

So basically NewHOC is just a function that accepts a component and returns a new component that renders the component passed in. We usually use this pattern to enhance components and share logic or data.

You can read about HOCS in the docs and I also recommend reading about the difference between react elements and components

I wrote an article about the different ways and patterns of sharing logic in react.

Solution 2 - Javascript

I did encounter this error too because I didn't use the correct snytax at routing. This was in my App.js under the <Routes> section:

False:

<Route path="/movies/list" exact element={ MoviesList } />

Correct:

<Route path="/movies/list" exact element={ <MoviesList/> } />

So now the MoviesList is recognized as a component.

Solution 3 - Javascript

In my case i forgot to add the () after the function name inside the render function of a react component

public render() {
       let ctrl = (
           <>
                <div className="aaa">
                    {this.renderView}
                </div>
            </>
       ); 

       return ctrl;
    };


    private renderView() : JSX.Element {
        // some html
    };

Changing the render method, as it states in the error message to

        <div className="aaa">
            {this.renderView()}
        </div>

fixed the problem

Solution 4 - Javascript

I encountered this error while following the instructions here: https://reactjs.org/docs/add-react-to-a-website.html

Here is what I had:

ReactDOM.render(Header, headerContainer);

It should be:

ReactDOM.render(React.createElement(Header), headerContainer);

Solution 5 - Javascript

Adding to sagiv's answer, we should create the parent component in such a way that it can consist all children components rather than returning the child components in the way you were trying to return.

Try to intentiate the parent component and pass the props inside it so that all children can use it like below

const NewComponent = NewHOC(Movie);

Here NewHOC is the parent component and all its child are going to use movie as props.

But any way, you guyd6 have solved a problem for new react developers as this might be a problem that can come too and here is where they can find the solution for that.

Solution 6 - Javascript

I had this error too. The problem was how to call the function.

Wrong Code:

const Component = () => {
    const id = ({match}) => <h2>Test1: {match.params.id}</h2>
    return <h1>{id}</h1>;
};

Whereas id is a function, So:

Correct code:

return <h1>{id()}</h1>;

Solution 7 - Javascript

I was able to resolve this by using my calling my high order component before exporting the class component. My problem was specifically using react-i18next and its withTranslation method, but here was the solution:

export default withTranslation()(Header);

And then I was able to call the class Component as originally I had hoped:

<Header someProp={someValue} />

Solution 8 - Javascript

In my case, I was transport class component from parent and use it inside as a prop var, using typescript and Formik, and run well like this:

Parent 1

import Parent2 from './../components/Parent2/parent2'
import Parent3 from './../components/Parent3/parent3'

export default class Parent1 extends React.Component {
  render(){
    <React.Fragment>
      <Parent2 componentToFormik={Parent3} />
    </React.Fragment>
  }
}

Parent 2

export default class Parent2 extends React.Component{
  render(){
    const { componentToFormik } = this.props
    return(
    <Formik 
      render={(formikProps) => {
        return(
          <React.fragment>
            {(new componentToFormik(formikProps)).render()}
          </React.fragment>
        )
      }}
    />
    )
  }
}

Solution 9 - Javascript

it also happens when you call a function from jsx directly rather than in an event. like

it will show the error if you write like

<h1>{this.myFunc}<h2>

it will go if you write:

<h1 onClick={this.myFunc}>Hit Me</h1>

Solution 10 - Javascript

I was getting this from webpack lazy loading like this

import Loader from 'some-loader-component';
const WishlistPageComponent = loadable(() => import(/* webpackChunkName: 'WishlistPage' */'../components/WishlistView/WishlistPage'), {
  fallback: Loader, // warning
});
render() {
    return <WishlistPageComponent />;
}


// changed to this then it's suddenly fine
const WishlistPageComponent = loadable(() => import(/* webpackChunkName: 'WishlistPage' */'../components/WishlistView/WishlistPage'), {
  fallback: '', // all good
});    

Solution 11 - Javascript

What would be wrong with doing;

<div className="" key={index}>
   {i.title}
</div>

[/*Use IIFE */]

{(function () {
     if (child.children && child.children.length !== 0) {
     let menu = createMenu(child.children);
     console.log("nested menu", menu);
     return menu;
   }
 })()}

Solution 12 - Javascript

In my case I forgot to remove this part '() =>'. Stupid ctrl+c+v mistake.

const Account = () => ({ name }) => {

So it should be like this:

const Account = ({ name }) => {

Solution 13 - Javascript

In my case

<Link key={uuid()} to="#" className="tag">
  {post.department_name.toString}
</Link>

changed with

<Link key={uuid()} to="#" className="tag">
  {post.department_name.toString()}
</Link>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlearnerView Question on Stackoverflow
Solution 1 - JavascriptSagiv b.gView Answer on Stackoverflow
Solution 2 - JavascriptChickenmanView Answer on Stackoverflow
Solution 3 - JavascriptStefan MichevView Answer on Stackoverflow
Solution 4 - JavascriptBL1133View Answer on Stackoverflow
Solution 5 - JavascriptVishal GuptaView Answer on Stackoverflow
Solution 6 - JavascriptParisaNView Answer on Stackoverflow
Solution 7 - JavascriptgreatgumzView Answer on Stackoverflow
Solution 8 - JavascriptRicardo Tamarán Núñez MonzónView Answer on Stackoverflow
Solution 9 - JavascriptThat's EnamView Answer on Stackoverflow
Solution 10 - JavascriptOZZIEView Answer on Stackoverflow
Solution 11 - JavascriptJoe MwaView Answer on Stackoverflow
Solution 12 - JavascriptMichalPrView Answer on Stackoverflow
Solution 13 - JavascriptRahul ShakyaView Answer on Stackoverflow