Created
September 14, 2019 15:34
-
-
Save zoonderkins/57b7f219f30c81e26bde4afff16693c6 to your computer and use it in GitHub Desktop.
Fix-Home-JS-classed-React GraphQL App (MERNG): #3 Displaying Posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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.
Cheers mate!
thank you. helped break the stalemate.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It doesn't work for me.
Expected an assignment or function call and instead saw an expression no-unused-expressions