Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React路由器未呈现组件,表示缺少返回语句_Reactjs_Router - Fatal编程技术网

Reactjs React路由器未呈现组件,表示缺少返回语句

Reactjs React路由器未呈现组件,表示缺少返回语句,reactjs,router,Reactjs,Router,我遇到如下错误:未捕获错误:路由(…):渲染未返回任何内容。这通常意味着缺少返回语句。 import React from 'react'; import {BrowserRouter, Switch, Route, ReactDom} from 'react-router-dom' import ProductsDisplay from './ProductsDisplay' import Home from './Home' import Contact from './Contact'

我遇到如下错误:未捕获错误:路由(…):渲染未返回任何内容。这通常意味着缺少返回语句。

import React from 'react';
import {BrowserRouter, Switch, Route, ReactDom} from 'react-router-dom'
import ProductsDisplay from './ProductsDisplay'
import Home from './Home'
import Contact from './Contact'

const Main = () => {
    return (
    <main>

            <Switch>
                <Route exact path="/" component={Home}/>
                <Route exact path="/contact" component={Contact}/>
                <Route exact path="/monitors" render={() => {<ProductsDisplay productCategory="monitors"/>}}/>
                <Route exact path="/computers" render={() => {<ProductsDisplay productCategory="computers"/>}}/>
            </Switch>

    </main>
    );
}

export default Main;
从“React”导入React;
从“react router dom”导入{BrowserRouter,Switch,Route,react dom}
从“/ProductsDisplay”导入产品显示
从“./主页”导入主页
从“./Contact”导入联系人
常量Main=()=>{
返回(
{}}/>
{}}/>
);
}
导出默认主;

我确信返回语句在所有组件中都有,你知道这是什么吗

您没有从传递给render prop的方法返回任何内容。试着像这样删除
{}
括号

<Route exact path="/monitors" render={() => <ProductsDisplay productCategory="monitors"/>}/>
<Route exact path="/computers" render={() => <ProductsDisplay productCategory="computers"/>}/>
<Route exact path="/monitors" render={() => {return <ProductsDisplay productCategory="monitors"/>}}/>
<Route exact path="/computers" render={() => {return <ProductsDisplay productCategory="computers"/>}}/>

您不会从传递给render prop的方法返回任何内容。试着像这样删除
{}
括号

<Route exact path="/monitors" render={() => <ProductsDisplay productCategory="monitors"/>}/>
<Route exact path="/computers" render={() => <ProductsDisplay productCategory="computers"/>}/>
<Route exact path="/monitors" render={() => {return <ProductsDisplay productCategory="monitors"/>}}/>
<Route exact path="/computers" render={() => {return <ProductsDisplay productCategory="computers"/>}}/>

监视器
计算机
路由渲染属性中缺少Return语句。您需要返回
反应组件
反应元素

<Switch>
<Route exact path="/" component={Home}/>
<Route exact path="/contact" component={Contact}/>
<Route exact path="/monitors" render={() => {return <ProductsDisplay productCategory="monitors"/>}}/>
<Route exact path="/computers" render={() => {return <ProductsDisplay productCategory="computers"/>}}/>
</Switch>

{return}}/>
{return}}/>
或者使用ECMA6默认返回语法

<Switch>
<Route exact path="/" component={Home}/>
<Route exact path="/contact" component={Contact}/>
<Route exact path="/monitors" render={() => (<ProductsDisplay productCategory="monitors"/>)}/>
<Route exact path="/computers" render={() => (<ProductsDisplay productCategory="computers"/>)}/>
</Switch>

()}/>
()}/>

监视器
计算机
路由渲染属性中缺少返回语句。您需要返回
反应组件
反应元素

<Switch>
<Route exact path="/" component={Home}/>
<Route exact path="/contact" component={Contact}/>
<Route exact path="/monitors" render={() => {return <ProductsDisplay productCategory="monitors"/>}}/>
<Route exact path="/computers" render={() => {return <ProductsDisplay productCategory="computers"/>}}/>
</Switch>

{return}}/>
{return}}/>
或者使用ECMA6默认返回语法

<Switch>
<Route exact path="/" component={Home}/>
<Route exact path="/contact" component={Contact}/>
<Route exact path="/monitors" render={() => (<ProductsDisplay productCategory="monitors"/>)}/>
<Route exact path="/computers" render={() => (<ProductsDisplay productCategory="computers"/>)}/>
</Switch>

()}/>
()}/>

如果您的组件导入不正确,可能会发生这种情况。如果您的组件导入不正确,可能会发生这种情况。我认为ES6部分是arrow函数(在两个示例中都使用),而不是没有
{
的隐式
返回。我认为ES6部分是arrow函数(在两个示例中都使用),不是没有
{
的隐式
返回
。这样做了谢谢!里亚杰·汗的答案也有效,但我只能接受一个答案;)这样做了谢谢!里亚杰·汗的答案也有效,但我只能接受一个答案;)