0

我正在尝试在 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它应该作为当前状态导入。

任何帮助以及建议或批评将不胜感激。先感谢您!

4

2 回答 2

0

如果组件带有剥离的代码/内容

export default function StoriesHOC(props) {
  return (
    <Query query={getStories}>
      {({ data }) => {
        console.log(data)
        return <div>fake content</div>
      }
    </Query>
  )
}

正确呈现,然后查询正常,问题出在已删除的部分。将它们一一恢复以​​查找/隔离问题的真正根源。

于 2018-09-06T15:20:21.337 回答
0

我发现了我的问题,它是一个过时的 npm 安装,react-apollo, graphql-tag, and apollo-boost.这听起来很明显,这应该是我看的第一个地方,但我两周前刚刚更新了它们!

对于路过的人,这里是对我有用的软件包版本的片段:

"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"
 }
于 2018-09-06T20:52:24.480 回答