Javascript ReactDOM需要字符串但返回对象
我正在使用React和Graphql运行一个应用程序。我得到以下错误:Javascript ReactDOM需要字符串但返回对象,javascript,reactjs,graphql,graphql-js,Javascript,Reactjs,Graphql,Graphql Js,我正在使用React和Graphql运行一个应用程序。我得到以下错误: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. 运行以下代码时: import React from 'react'; import ReactDOM from 'react-dom'; import { A
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
运行以下代码时:
import React from 'react';
import ReactDOM from 'react-dom';
import { ApolloProvider } from 'react-apollo';
import { ApolloClient } from 'apollo-client';
import { HttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import Routes from './routes';
import registerServiceWorker from './registerServiceWorker';
const client = new ApolloClient({
link: new HttpLink({ uri: 'http://localhost:8080' }),
cache: new InMemoryCache(),
});
const App = (
<ApolloProvider client={client}>
<Routes />
</ApolloProvider>
);
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
和Home.js
import React from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
const Home = ({ data: { loading, allUsers } }) =>
(loading ? null : allUsers.map(u => <h1 key={u.id}>{u.email}</h1>));
const allUsersQuery = gql`
{
allUsers {
id
email
}
}
`;
export default graphql(allUsersQuery)(Home);
从“React”导入React;
从'react apollo'导入{graphql};
从“graphql标签”导入gql;
常量Home=({data:{loading,alluser}})=>
(加载?null:allUsers.map(u=>{u.email}));
常量allUsersQuery=gql`
{
诱惑者{
身份证件
电子邮件
}
}
`;
导出默认图形ql(allUsersQuery)(主页);
我不知道确切的原因,也许有人可以插话,但你不能有这样的组件:
const App = (
<ApolloProvider client={client}>
<Routes />
</ApolloProvider>
);
const应用=(
);
如果使用ReactDOM.render(,…)
如果按如下所示将其更改为功能组件,则应能正常工作:
const App = () => (
<ApolloProvider client={client}>
<Routes />
</ApolloProvider>
);
const-App=()=>(
);
但是,您也可以只执行
ReactDOM.render(App,…)
,而不将其更改为函数。什么是
@Tony刚刚在Routes目录中用更多代码对其进行了更新。您是对的,修复了它。非常感谢。如果有人知道原因并能解释,那就太好了。我用一些额外的信息更新了我的答案@user081608
const App = () => (
<ApolloProvider client={client}>
<Routes />
</ApolloProvider>
);