Skip to content

Instantly share code, notes, and snippets.

@davidyeiser
Created April 2, 2018 13:02
Show Gist options
  • Save davidyeiser/cb7766483125b8d3a70f2ef34ff08e05 to your computer and use it in GitHub Desktop.
Save davidyeiser/cb7766483125b8d3a70f2ef34ff08e05 to your computer and use it in GitHub Desktop.
Example Post component for React
import Link from 'next/link'
import dateFormat from 'dateformat'
import Markdown from 'react-markdown'
class Post extends React.Component {
render() {
const {
title,
content,
publish_date,
slug,
id
} = this.props
const hasId = !!id ? true : false
const permalink = hasId ? '/post/' + id + '/' + slug : false
return (
<div>
{!!permalink ?
<Link href={permalink}>
<a title="Permalink for this note">
{title && <Title>{title}</Title> : ''}
{publish_date &&
<time dateTime={dateFormat(publish_date, 'isoDateTime')}>{dateFormat(publish_date, 'mmmm d, yyyy')}</time>
}
</a>
</Link> :
<div>
{title && <Title>{title}</Title>}
{publish_date &&
<time dateTime={dateFormat(publish_date, 'isoDateTime')}>{dateFormat(publish_date, 'mmmm d, yyyy')}</time>
}
</div>
}
{content && <Markdown source={content} /> }
</div>
)
}
}
export default Post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment