简体   繁体   中英

Can't render my component, Expected an assignment or function call and instead saw an expression in React

I written my code following a udemy course. I'm unable to render the Layout component's content is there anything I missed or any syntax mistakes that needs to be corrected to sort this out?

const Aux = (props) => {props.children}

export default Aux;

import React,{Component} from 'react'
import Layout from './components/Layout/Layout';

class App extends Component
 {
  render() {
    return (
      <div>
        <Layout>
          <p>Hai.. Is this working ? </p>
        </Layout>
      </div>
    );
  }
}

export default App;

import React from 'react';
import Aux from '../../hoc/Auxx';

const layout = (props) =>(
    <Aux>
    <div>Toolbar,Sidebar,Backdrop</div>
    <main>
        {props.children}
    </main>
    </Aux>
    
);

export default layout;

You problem is that Aux is a blank component.

When you use the syntax const Aux = (props) => {props.children} you actually return nothing!

You see, javascript thinks that { } is the function itself and not return your props.children . Just remove the brackets:

const Aux = (props) => props.children;

I've modified your code as below:

const Aux = (props) => props.children // removed the {} so that it can return the children 

export default Aux;

import React,{Component} from 'react'
import Layout from './components/Layout/Layout';

class App extends Component
 {
  render() {
    return (
      <div>
        <Layout>
          <p>Hai.. Is this working ? </p>
        </Layout>
      </div>
    );
  }
}

export default App;

import React from 'react';
import Aux from '../../hoc/Auxx';

const Layout = (props) =>( //changed the layout to Layout: it needs to be capitalized
    <Aux>
    <div>Toolbar,Sidebar,Backdrop</div>
    <main>
        {props.children}
    </main>
    </Aux>
    
);

export default layout;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM