繁体   English   中英

React - 错误:应用程序(...):渲染没有返回任何内容。 这通常意味着缺少返回语句。 或者,不渲染任何内容,返回 null

[英]React - Error: App(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null

我正在使用 MERN 和 GraphQL Apollo 创建一个完整的堆栈应用程序。 为了将后端连接到客户端,我使用的是 Apollo Client。 这是代码-

ApolloProvider.js

import React from "react";
import App from "./App";
import {
  ApolloClient,
  createHttpLink,
  InMemoryCache,
  ApolloProvider,
} from "@apollo/client";

const httpLink = createHttpLink({ uri: "http://localhost:5000" });

const client = new ApolloClient({
  link: httpLink,
  cache: new InMemoryCache(),
});

export default (
  <ApolloProvider client={client}>
    <App />
  </ApolloProvider>
);

索引.js

import ReactDOM from "react-dom";
import reportWebVitals from "./reportWebVitals";
import ApolloProvider from "./ApolloProvider";

ReactDOM.render(ApolloProvider, document.getElementById("root"));

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint.
reportWebVitals();

应用程序.js

import React from "react";
import { BrowserRouter as Router, Route } from "react-router-dom";
import { Container } from "semantic-ui-react";

import "semantic-ui-css/semantic.min.css";
import "./App.css";

import MenuBar from "./components/MenuBar";
import Home from "./pages/Home";
import Login from "./pages/Login";
import Register from "./pages/Register";

function App() {
  <Router>
    <Container>
      <MenuBar />
      <Route exact path="/" component={Home} />
      <Route exact path="/login" component={Login} />
      <Route exact path="/register" component={Register} />
    </Container>
  </Router>;
}

export default App;

ReactDOM.render(ApolloProvider, document.getElementById("root")); 错误来了 - '没有从渲染中返回任何东西。 这通常意味着缺少返回语句。 或者,不渲染任何内容,返回 null。

谁能帮忙解决这个问题? 编辑:堆栈跟踪 - 在此处输入图像描述

编辑:看起来问题一直在你的App.js中。 您不返回应用程序的 JSX。 将其添加到:

function App() {
  return (
     <Router>
        <Container>
        <MenuBar />
        <Route exact path="/" component={Home} />
        <Route exact path="/login" component={Login} />
        <Route exact path="/register" component={Register} />
        </Container>
     </Router>
  );
}

原答案:

您需要实际渲染ReactDOM.render中的元素,即

ReactDOM.render(<ApolloProvider/>, document.getElementById("root"));

同样在你的 apollo 提供者中,返回一个功能组件,而不是纯 JSX。 不确定这是否重要,但可能会有所帮助,即

const ApolloApp = () => (
  <ApolloProvider client={client}>
    <App />
  </ApolloProvider>
);

export default ApolloApp;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,不渲染任何内容,返回 null 反应错误:渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,什么也不渲染,返回 null Home(…):渲染未返回任何内容。 这通常意味着缺少return语句。 或者,不渲染任何内容,则返回null unboundStoryFn(…):渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,什么也不渲染,返回 null React Component Array Map-渲染未返回任何内容。 这通常意味着缺少return语句。 或者,不渲染任何内容,则返回null 出现此错误的学校项目:渲染未返回任何内容。 这通常意味着缺少 return 语句。 或者,不渲染任何内容,返回 null 我收到此错误 &gt;&gt; 渲染未返回任何内容。 这通常意味着缺少 return 语句。 或者,不渲染任何内容,返回 null 错误:StateProvider(...):渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,什么也不渲染,返回 null 错误:聊天(...):渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,不渲染任何内容,返回 null Index.js 错误:UserForm(…):没有从渲染返回。 这通常意味着缺少 return 语句。 或者,什么也不渲染,返回 null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM