簡體   English   中英

無法渲染我的組件,需要賦值或 function 調用,而是在 React 中看到了一個表達式

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

我按照 udemy 課程編寫了代碼。 我無法呈現 Layout 組件的內容是否有任何我遺漏的內容或任何需要糾正的語法錯誤來解決這個問題?

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;

你的問題是Aux是一個空白組件。

當你使用語法const Aux = (props) => {props.children}你實際上什么都不返回!

你看, javascript 認為{ }是 function 本身,而不是返回你的props.children 只需刪除括號:

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

我已將您的代碼修改如下:

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM