React - Error: App(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return 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>
);
index.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();
App.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"));
出现错误 - 'Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.'
谁能帮忙解决这个问题?
编辑:堆栈跟踪 -
编辑:看起来问题一直都在您的 App.js
中。您没有 return 您应用的 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 提供程序中,return 一个功能组件,而不是纯 JSX。不确定这是否重要,但可能会有所帮助,即
const ApolloApp = () => (
<ApolloProvider client={client}>
<App />
</ApolloProvider>
);
export default ApolloApp;
我正在使用 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>
);
index.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();
App.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"));
出现错误 - 'Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.'
谁能帮忙解决这个问题?
编辑:堆栈跟踪 -
编辑:看起来问题一直都在您的 App.js
中。您没有 return 您应用的 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 提供程序中,return 一个功能组件,而不是纯 JSX。不确定这是否重要,但可能会有所帮助,即
const ApolloApp = () => (
<ApolloProvider client={client}>
<App />
</ApolloProvider>
);
export default ApolloApp;