0

I am trying to render my HOC function in ReactJS, but it is giving me an error. Here are a few snippets (I have cut out the unrelated stuff for space and clarity):

In my app.js component I am importing my HOC as such:

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>
   );
 }
}

Then going into the <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>
 );
}

EDIT: Here is the StoryShow Component as it too is being exported as a default function.

  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>
  )
 }

The actual GraphQL query, import { getStories } from "../../Queries/Queries"; is being carried out in another file and is formatted as such:

 import gql from "graphql-tag";

 export const getStories = gql`
   query getStories {
      stories {
        id
        title
        author
        tagline
      }
     }
   `;

However, the actual error displayed in the chrome browser is:

 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`.

It later gives the line at where it stops which is <Query query={getStories}> seen earlier in the StoriesHOC function. That being said, it would seem my issue is stemming from this line alone, but while I've been all of the board with trying to solve the issue, my best guess is only that I am doing something wrong in my GraphQL query, however when I check the IDE in the backend, it renders perfectly.

There is also the issue of trying to figure out if I exported the imported the function properly in my app.js file, but I know that because I exported using the export default function myFunc that it should be imported as currently state.

Any help would be greatly appreciated, as well as advice or critques. Thank you in advance!

Lullaby
  • 127
  • 1
  • 3
  • 11
  • Error seems to be JSX tag related - `StoryShow` is exported? – xadm Sep 06 '18 at 07:23
  • @xadm As far as I can tell, yes, I edited the post to show I was exporting it. – Lullaby Sep 06 '18 at 12:39
  • Remove all 'body' from `` in `StoriesHOC` - render some html, simple `
    to check if it's related to child components or query. Queries can be directly imported from `*.graphql` files.
    – xadm Sep 06 '18 at 12:55
  • I did as you said, and the StoriesHOC component renders no problem. I believe my error is in the Query component at , because when I get the error in the browser, the debugger redirects me to that line – Lullaby Sep 06 '18 at 15:03

2 Answers2

0

If component with stripped code/content

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

renders correctly then query is OK and problems is in removed parts. Recover them one by one to find/isolate real source of problems.

xadm
  • 8,219
  • 3
  • 14
  • 25
  • I'm sorry, perhaps I did not mention it clearly enough. I know that the source of the error is the actual ```query={getStories}``` line in the `````` component, so I know where the problem is, but I don't know what I am doing wrong. – Lullaby Sep 06 '18 at 15:53
  • try to define `getStories` directly in the same file, w/o importing. This error usually is related to virtual tree/nodes/elements - not params/props. – xadm Sep 06 '18 at 17:32
  • I have done what you recommended, but the error still persists, I am fresh out of ideas at this point. Any recommendations? I can link my Github code if you like – Lullaby Sep 06 '18 at 20:30
  • try `getStories` w/o `query getStories {` – xadm Sep 06 '18 at 20:39
  • The issue has been resolved. Thank you for your help! – Lullaby Sep 06 '18 at 20:52
  • [import queries from files](https://github.com/apollographql/graphql-tag#webpack-preprocessing-with-graphql-tagloader) - usable with fragments – xadm Sep 06 '18 at 21:19
0

I found my problem, it was an outdated npm installs for react-apollo, graphql-tag, and apollo-boost. It sounds pretty obvious that that should have been the first place that I looked, but I had literally just updated them two weeks ago!

For the people passing by, here is a snippet off the package versions that are working for me:

"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"
 }
Lullaby
  • 127
  • 1
  • 3
  • 11