Skip to content

Instantly share code, notes, and snippets.

@zoonderkins
Created September 14, 2019 15:34
Show Gist options
  • Save zoonderkins/57b7f219f30c81e26bde4afff16693c6 to your computer and use it in GitHub Desktop.
Save zoonderkins/57b7f219f30c81e26bde4afff16693c6 to your computer and use it in GitHub Desktop.
Fix-Home-JS-classed-React GraphQL App (MERNG): #3 Displaying Posts
import React from 'react'
import { useQuery } from '@apollo/react-hooks'
import gql from 'graphql-tag'
import { Grid, Divider } from 'semantic-ui-react'
import PostCard from '../components/PostCard'
function Home() {
let posts = ''
const { loading, data } = useQuery(FETCH_POSTS_QUERY)
console.log(`Loading: ${loading}`)
console.log(data)
if (data) {
posts = { data: data.getPosts }
}
return (
<Grid columns={3}>
<Grid.Row className="page-title">
<h1>Recent Posts</h1>
</Grid.Row>
{loading ? (
<h1>Loading posts..</h1>
) : (
posts.data &&
posts.data.map(post => (
<Grid.Column key={post.id} style={{ marginBottom: 20 }}>
<PostCard post={post} />
</Grid.Column>
))
)}
<Grid.Row></Grid.Row>
</Grid>
)
}
const FETCH_POSTS_QUERY = gql`
{
getPosts {
id
body
createdAt
username
likeCount
likes {
username
}
commentCount
comments {
id
username
createdAt
body
}
}
}
`
export default Home
@josuerodcat90
Copy link

thanks a lot sir, worked perfectly!

@DreamworksTeam
Copy link

thanks man

@nicolecastillo
Copy link

It worked, thank you!

@siumhossain
Copy link

i have no word !!!! how can i appreciate your work...thanks man!

@benyam7
Copy link

benyam7 commented Jan 10, 2020

Thank you!

@earthddx
Copy link

earthddx commented Feb 4, 2020

thanks mate 👍

@terrygreen0606
Copy link

It doesn't work for me.

Expected an assignment or function call and instead saw an expression no-unused-expressions

@AndyDeNike
Copy link

I notice that when I make a change to the react code, save and the auto refresh does not display the posts properly.

As soon as I manually refresh it loads properly. Any idea why this would be the case?

when console.log(error) i get:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5000/. (Reason: CORS request did not succeed).
OPTIONS http://localhost:5000/ net::ERR_CONNECTION_REFUSED
Home.js:11 Error: Network error: Failed to fetch

Should I just not care and move on with the tutorial? Thoughts? Thank you for your time.

@danroche10
Copy link

Cheers mate!

@mprkatti
Copy link

thank you. helped break the stalemate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment