-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts.js
52 lines (46 loc) · 1.19 KB
/
posts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import Navigation from '../components/Navigation'
import React, { Component, Fragment } from 'react'
import axios from 'axios'
import Link from 'next/link'
import Head from 'next/head'
export default class extends Component {
// Resolve promise and set initial props.
static async getInitialProps () {
const siteurl = 'wp.test';
// Make request for posts.
const response = await axios.get( 'http://' + siteurl + '/wp-json/wp/v2/posts');
// Return response to posts object in props.
return {
posts: response.data
}
}
render() {
return (
<Fragment>
<Navigation/>
<Head>
<title>Blog Page</title>
<meta name="description" content="Headless WP Blog Page" />
<meta charSet="utf-8" />
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
</Head>
<h1>Our Posts Page!</h1>
<ul>
{
this.props.posts.map( post => {
return (
<li key={ post.id }>
<Link href={ `/posts/${ post.slug }` }>
<a href={ `/posts/${ post.slug }` } dangerouslySetInnerHTML={ {
__html: post.title.rendered
} } />
</Link>
</li>
)
})
}
</ul>
</Fragment>
)
}
}