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

后端 未结 8 2220
醉话见心
醉话见心 2020-12-04 20:54

I have written a Higher Order Component:

import React from \'react\';


const NewHOC = (PassedComponent) => {
    return class extends React.Component {
          


        
相关标签:
8条回答
  • 2020-12-04 21:24

    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.

    0 讨论(0)
  • 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} />
    
    0 讨论(0)
  • 2020-12-04 21:28

    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>
            )
          }}
        />
        )
      }
    }
    
    0 讨论(0)
  • 2020-12-04 21:30

    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>
    
    0 讨论(0)
  • 2020-12-04 21:31

    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

    0 讨论(0)
  • 2020-12-04 21:39

    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;
       }
     })()}
    
    0 讨论(0)
提交回复
热议问题