Created
September 14, 2018 09:47
-
-
Save pex/f525316e128e31dd255a6ef58a4cbb9c to your computer and use it in GitHub Desktop.
Next.js + Prismic Intro
This file contains hidden or 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 Link from 'next/link' | |
import { RichText } from 'prismic-reactjs' | |
import { fetchBlogPosts, linkResolver } from '../lib/prismic.js' | |
import withPrismic from '../lib/with-prismic.js' | |
class BlogIndex extends React.Component { | |
static async getInitialProps (ctx.prismic) { | |
const posts = await fetchBlogPosts(ctx.prismic) | |
return { posts } | |
} | |
render () { | |
return ( | |
<ul> | |
{posts.map(post => ( | |
<li key={post.id}> | |
<h2>{RichText.asText(post.data.title)}</h2> | |
<Link href={linkResolver(post.uid)}> | |
<a>View Article</a> | |
</Link> | |
</li> | |
))} | |
</ul> | |
) | |
} | |
} | |
export default withPrismic(BlogIndex) |
This file contains hidden or 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
export const linkResolver = (doc) => { | |
if (doc.type === 'blog_article') { | |
return `/article?uid=${doc.uid}` | |
} | |
return '/' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment