使用 GraphQL 和 ReactJS 未定义导入的 <Query> 组件
imported <Query> component is undefined with GraphQL and ReactJS
我试图在 ReactJS 中呈现我的 HOC 函数,但它给我一个错误。以下是一些片段(为了 space 和清楚起见,我删除了不相关的内容):
在我的 app.js
组件中,我正在导入我的 HOC:
import React, { Component } from "react";
import "./App.css";
import { Route, Switch, BrowserRouter as Router } from "react-router-dom";
import Home from "../Home/Home";
import StoriesHOC from "../Story/GetAllStories/Stories";
export default class App extends Component {
render() {
return (
<Router>
<div>
<Navigation />
<Switch>
<Route exact path="/" component={Home} />
<Route
path="/all_stories"
render={() => {
return <StoriesHOC />;
}}
/>
</Switch>
</div>
</Router>
);
}
}
然后进入 <StoriesHOC />
:
import React from "react";
import StoryShow from "../StoryShow/StoryShow";
import { Query, Fetching, Error } from "react-apollo";
import { getStories } from "../../Queries/Queries";
function Stories({ stories, getStoriesQuery }) {
return (
<div>
<div className="stories_header">
<h2>All Stories</h2>
</div>
<div className="stories-container">
{stories.map(story => (
<StoryShow
key={story.id}
story={story}
getStoriesQuery={getStoriesQuery}
/>
))}
</div>
</div>
);
}
export default function StoriesHOC(props) {
return (
<Query query={getStories}>
{({ loading, error, data }) => {
console.log(data)
if (error) return <Error />
if (loading || !data) return <Fetching />
return <Stories
{...props}
getStoriesQuery={getStories}
stories={(data && data.stories) || []}/> }}
</Query>
);
}
编辑:这是 StoryShow 组件,因为它也作为默认函数导出。
import React from "react";
import { Link } from 'react-router-dom'
export default function StoryShow({
story: {id, title, author, tagline, summary} = {}
}) {
return (
<div>
<h1>{title}</h1>
<h2>{tagline}</h2>
<h3>{author}</h3>
<p>{summary}</p>
<Link exact to={'/story/' + id}>Read More</Link>
</div>
)
}
实际的 GraphQL 查询,import { getStories } from "../../Queries/Queries";
正在另一个文件中执行,格式如下:
import gql from "graphql-tag";
export const getStories = gql`
query getStories {
stories {
id
title
author
tagline
}
}
`;
但是chrome浏览器中实际显示的错误是:
Element type is invalid: expected a string (for built-in components)
or a class/function (for composite components) but got: undefined. You
likely forgot to export your component from the file it's defined in,
or you might have mixed up default and named imports.
Check the render method of `StoriesHOC`.
它稍后会给出它停止的行,这是 <Query query={getStories}>
之前在 StoriesHOC 函数中看到的。 话虽这么说,但我的问题似乎仅源于这一行,但是虽然我一直在努力解决这个问题,但我最好的猜测只是我我在我的 GraphQL 查询中做错了,但是当我在后端检查 IDE 时,它完美呈现。
还有一个问题是试图弄清楚我是否在我的 app.js 文件中正确导出了导入的函数,但我知道因为我使用 export default function myFunc
导出它应该是导入为当前状态。
任何帮助,以及建议或批评,我们将不胜感激。提前致谢!
If 组件剥离 code/content
export default function StoriesHOC(props) {
return (
<Query query={getStories}>
{({ data }) => {
console.log(data)
return <div>fake content</div>
}
</Query>
)
}
正确呈现然后查询正常,问题出在删除的部分。将它们一一恢复到 find/isolate 真正的 问题根源。
我发现了我的问题,这是 react-apollo, graphql-tag, and apollo-boost.
的过时 npm 安装 听起来很明显,那应该是我首先查看的地方,但实际上我两周前刚刚更新了它们!
对于路过的人,这里是对我有用的软件包版本的一个片段:
"dependencies": {
"apollo-boost": "^0.1.15",
"cors": "^2.8.4",
"express": "^4.16.3",
"graphql": "^0.13.2",
"graphql-server-express": "^1.4.0",
"graphql-tag": "^2.9.2",
"react": "^16.5.0",
"react-apollo": "^2.1.11",
"react-dom": "^16.4.2",
"react-router-dom": "^4.3.1",
"react-scripts": "1.1.4"
}
我试图在 ReactJS 中呈现我的 HOC 函数,但它给我一个错误。以下是一些片段(为了 space 和清楚起见,我删除了不相关的内容):
在我的 app.js
组件中,我正在导入我的 HOC:
import React, { Component } from "react";
import "./App.css";
import { Route, Switch, BrowserRouter as Router } from "react-router-dom";
import Home from "../Home/Home";
import StoriesHOC from "../Story/GetAllStories/Stories";
export default class App extends Component {
render() {
return (
<Router>
<div>
<Navigation />
<Switch>
<Route exact path="/" component={Home} />
<Route
path="/all_stories"
render={() => {
return <StoriesHOC />;
}}
/>
</Switch>
</div>
</Router>
);
}
}
然后进入 <StoriesHOC />
:
import React from "react";
import StoryShow from "../StoryShow/StoryShow";
import { Query, Fetching, Error } from "react-apollo";
import { getStories } from "../../Queries/Queries";
function Stories({ stories, getStoriesQuery }) {
return (
<div>
<div className="stories_header">
<h2>All Stories</h2>
</div>
<div className="stories-container">
{stories.map(story => (
<StoryShow
key={story.id}
story={story}
getStoriesQuery={getStoriesQuery}
/>
))}
</div>
</div>
);
}
export default function StoriesHOC(props) {
return (
<Query query={getStories}>
{({ loading, error, data }) => {
console.log(data)
if (error) return <Error />
if (loading || !data) return <Fetching />
return <Stories
{...props}
getStoriesQuery={getStories}
stories={(data && data.stories) || []}/> }}
</Query>
);
}
编辑:这是 StoryShow 组件,因为它也作为默认函数导出。
import React from "react";
import { Link } from 'react-router-dom'
export default function StoryShow({
story: {id, title, author, tagline, summary} = {}
}) {
return (
<div>
<h1>{title}</h1>
<h2>{tagline}</h2>
<h3>{author}</h3>
<p>{summary}</p>
<Link exact to={'/story/' + id}>Read More</Link>
</div>
)
}
实际的 GraphQL 查询,import { getStories } from "../../Queries/Queries";
正在另一个文件中执行,格式如下:
import gql from "graphql-tag";
export const getStories = gql`
query getStories {
stories {
id
title
author
tagline
}
}
`;
但是chrome浏览器中实际显示的错误是:
Element type is invalid: expected a string (for built-in components)
or a class/function (for composite components) but got: undefined. You
likely forgot to export your component from the file it's defined in,
or you might have mixed up default and named imports.
Check the render method of `StoriesHOC`.
它稍后会给出它停止的行,这是 <Query query={getStories}>
之前在 StoriesHOC 函数中看到的。 话虽这么说,但我的问题似乎仅源于这一行,但是虽然我一直在努力解决这个问题,但我最好的猜测只是我我在我的 GraphQL 查询中做错了,但是当我在后端检查 IDE 时,它完美呈现。
还有一个问题是试图弄清楚我是否在我的 app.js 文件中正确导出了导入的函数,但我知道因为我使用 export default function myFunc
导出它应该是导入为当前状态。
任何帮助,以及建议或批评,我们将不胜感激。提前致谢!
If 组件剥离 code/content
export default function StoriesHOC(props) {
return (
<Query query={getStories}>
{({ data }) => {
console.log(data)
return <div>fake content</div>
}
</Query>
)
}
正确呈现然后查询正常,问题出在删除的部分。将它们一一恢复到 find/isolate 真正的 问题根源。
我发现了我的问题,这是 react-apollo, graphql-tag, and apollo-boost.
的过时 npm 安装 听起来很明显,那应该是我首先查看的地方,但实际上我两周前刚刚更新了它们!
对于路过的人,这里是对我有用的软件包版本的一个片段:
"dependencies": {
"apollo-boost": "^0.1.15",
"cors": "^2.8.4",
"express": "^4.16.3",
"graphql": "^0.13.2",
"graphql-server-express": "^1.4.0",
"graphql-tag": "^2.9.2",
"react": "^16.5.0",
"react-apollo": "^2.1.11",
"react-dom": "^16.4.2",
"react-router-dom": "^4.3.1",
"react-scripts": "1.1.4"
}