mirror of
https://github.com/vercel/next-learn.git
synced 2026-06-26 06:06:03 +00:00
Add example
This commit is contained in:
5
example/pages/_app.js
Normal file
5
example/pages/_app.js
Normal file
@@ -0,0 +1,5 @@
|
||||
import '../styles/global.css'
|
||||
|
||||
const App = ({ Component, pageProps }) => <Component {...pageProps} />
|
||||
|
||||
export default App
|
||||
3
example/pages/api/hello.js
Normal file
3
example/pages/api/hello.js
Normal file
@@ -0,0 +1,3 @@
|
||||
export default (req, res) => {
|
||||
res.status(200).json({ text: 'Hello' })
|
||||
}
|
||||
54
example/pages/index.js
Normal file
54
example/pages/index.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import Head from 'next/head'
|
||||
import Layout, { siteTitle } from '../components/Layout'
|
||||
import utilStyles from '../styles/utils.module.css'
|
||||
import { getSortedPostsData } from '../lib/posts'
|
||||
import Link from 'next/link'
|
||||
import Date from '../components/Date'
|
||||
|
||||
const Home = ({ allPostsData }) => (
|
||||
<Layout home>
|
||||
<Head>
|
||||
<title>{siteTitle}</title>
|
||||
</Head>
|
||||
<section className={utilStyles.headingMd}>
|
||||
<p>
|
||||
Hello, I’m <strong>Shu</strong>. I write code at{' '}
|
||||
<a href="https://zeit.co">ZEIT</a>, the team behind{' '}
|
||||
<a href="https://nextjs.org/">Next.js</a>. You can contact me via{' '}
|
||||
<a href="https://twitter.com/chibicode">Twitter</a> or{' '}
|
||||
<a href="mailto:chibicode@zeit.co">email</a>.
|
||||
</p>
|
||||
<p>
|
||||
(This is a sample website - you’ll be building a site like this on{' '}
|
||||
<a href="https://nextjs.org/learn">our Next.js tutorial</a>.)
|
||||
</p>
|
||||
</section>
|
||||
<section className={`${utilStyles.headingMd} ${utilStyles.padding1px}`}>
|
||||
<h2 className={utilStyles.headingLg}>Blog</h2>
|
||||
<ul className={utilStyles.list}>
|
||||
{allPostsData.map(({ id, date, title }) => (
|
||||
<li className={utilStyles.listItem}>
|
||||
<Link href="/posts/[id]" as={`/posts/${id}`}>
|
||||
<a>{title}</a>
|
||||
</Link>
|
||||
<br />
|
||||
<small className={utilStyles.lightText}>
|
||||
<Date dateString={date} />
|
||||
</small>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</section>
|
||||
</Layout>
|
||||
)
|
||||
|
||||
export async function getStaticProps() {
|
||||
const allPostsData = getSortedPostsData()
|
||||
return {
|
||||
props: {
|
||||
allPostsData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Home
|
||||
39
example/pages/posts/[id].js
Normal file
39
example/pages/posts/[id].js
Normal file
@@ -0,0 +1,39 @@
|
||||
import Layout from '../../components/Layout'
|
||||
import { getAllPostIds, getPostData } from '../../lib/posts'
|
||||
import Head from 'next/head'
|
||||
import Date from '../../components/Date'
|
||||
import utilStyles from '../../styles/utils.module.css'
|
||||
|
||||
const Post = ({ postData }) => (
|
||||
<Layout>
|
||||
<Head>
|
||||
<title>{postData.title}</title>
|
||||
</Head>
|
||||
<article>
|
||||
<h1 className={utilStyles.headingXl}>{postData.title}</h1>
|
||||
<div className={utilStyles.lightText}>
|
||||
<Date dateString={postData.date} />
|
||||
</div>
|
||||
<div dangerouslySetInnerHTML={{ __html: postData.contentHtml }} />
|
||||
</article>
|
||||
</Layout>
|
||||
)
|
||||
|
||||
export async function getStaticPaths() {
|
||||
const paths = getAllPostIds()
|
||||
return {
|
||||
paths,
|
||||
fallback: false
|
||||
}
|
||||
}
|
||||
|
||||
export async function getStaticProps({ params }) {
|
||||
const postData = await getPostData(params.id)
|
||||
return {
|
||||
props: {
|
||||
postData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Post
|
||||
Reference in New Issue
Block a user