我正在尝试在 ReactJS 中呈现我的 HOC 函数,但它给了我一个错误。以下是一些片段(为了空间和清晰度,我已经删除了不相关的内容):
在我的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
它应该作为当前状态导入。
任何帮助以及建议或批评将不胜感激。先感谢您!