File tree Expand file tree Collapse file tree 5 files changed +66
-9
lines changed Expand file tree Collapse file tree 5 files changed +66
-9
lines changed Original file line number Diff line number Diff line change
1
+ https://blog.reactjsfoundations.com/wp-json/wp/v2/posts/50
Original file line number Diff line number Diff line change 1
1
import { QueryClient , QueryClientProvider , useQuery } from 'react-query' ;
2
2
import { useState } from 'react' ;
3
3
import * as styles from './WpBlog.module.css' ;
4
+ import { useParams } from 'react-router-dom' ;
4
5
5
6
const queryClient = new QueryClient ( ) ;
6
7
@@ -19,13 +20,19 @@ export default function WpBlog(props) {
19
20
function ReactBlog ( props ) {
20
21
const [ totalPages , setTotalPages ] = useState ( 0 ) ;
21
22
const [ page , setPage ] = useState ( 1 ) ;
23
+ let { id } = useParams ( ) ;
22
24
25
+ let url ;
26
+
27
+ if ( id ) {
28
+ url = `https://blog.reactjsfoundations.com/wp-json/wp/v2/posts/${ id } /?per_page=${ props . posts } &page=${ page } ` ;
29
+ } else {
30
+ url = `https://blog.reactjsfoundations.com/wp-json/wp/v2/posts?per_page=${ props . posts } &page=${ page } ` ;
31
+ }
23
32
const { isLoading, error, data } = useQuery (
24
33
[ 'posts' , page ] ,
25
34
( ) => {
26
- return fetch (
27
- `https://blog.reactjsfoundations.com/wp-json/wp/v2/posts?per_page=${ props . posts } &page=${ page } `
28
- ) . then ( ( res ) => {
35
+ return fetch ( url ) . then ( ( res ) => {
29
36
setTotalPages ( parseInt ( res . headers . get ( 'x-wp-totalpages' ) ) ) ;
30
37
return res . json ( ) ;
31
38
} ) ;
@@ -66,6 +73,8 @@ function ReactBlog(props) {
66
73
className = { styles . post__content }
67
74
dangerouslySetInnerHTML = { { __html : post . content . rendered } }
68
75
/>
76
+
77
+ < hr />
69
78
</ div >
70
79
) ) }
71
80
</ div >
Original file line number Diff line number Diff line change
1
+ import { QueryClient , QueryClientProvider , useQuery } from 'react-query' ;
2
+ import { useState , useEffect } from 'react' ;
3
+ import * as styles from './WpBlog.module.css' ;
4
+ import { useParams } from 'react-router-dom' ;
5
+
6
+ export default function ReactBlog ( props ) {
7
+ let { id } = useParams ( ) ;
8
+ const [ post , setPost ] = useState ( ) ;
9
+ useEffect ( ( ) => {
10
+ if ( id ) {
11
+ const getPost = async ( ) => {
12
+ const response = await fetch (
13
+ `https://blog.reactjsfoundations.com/wp-json/wp/v2/posts/${ id } /`
14
+ ) ;
15
+ const data = await response . json ( ) ;
16
+ console . log ( data ) ;
17
+ setPost ( data ) ;
18
+ } ;
19
+ getPost ( ) ;
20
+ }
21
+ } , [ id ] ) ;
22
+
23
+ if ( post ) {
24
+ return (
25
+ < div className = { styles . blog__posts } >
26
+ < h1 className = { styles . blog__title } > { post . title . rendered } </ h1 >
27
+ < div key = { post . id } >
28
+ < section
29
+ className = { styles . post__content }
30
+ dangerouslySetInnerHTML = { { __html : post . content . rendered } }
31
+ />
32
+ </ div >
33
+ </ div >
34
+ ) ;
35
+ }
36
+ return 'Loading...' ;
37
+ }
Original file line number Diff line number Diff line change 1
1
import { QueryClient , QueryClientProvider , useQuery } from 'react-query' ;
2
2
import { useState } from 'react' ;
3
3
import * as styles from './WpBlog.module.css' ;
4
+ import { Link } from 'react-router-dom' ;
4
5
5
6
const queryClient = new QueryClient ( ) ;
6
7
@@ -56,16 +57,21 @@ function ReactBlog(props) {
56
57
{ data . map ( ( post ) => (
57
58
< div key = { post . id } >
58
59
< h2 >
59
- < section
60
- className = { styles . post__title }
61
- dangerouslySetInnerHTML = { { __html : post . title . rendered } }
62
- />
60
+ < Link to = { `/blog/${ post . id } ` } >
61
+ < section
62
+ className = { styles . post__title }
63
+ dangerouslySetInnerHTML = { { __html : post . title . rendered } }
64
+ />
65
+ </ Link >
63
66
</ h2 >
64
67
65
68
< section
66
69
className = { styles . post__content }
67
- dangerouslySetInnerHTML = { { __html : post . content . rendered } }
70
+ dangerouslySetInnerHTML = { { __html : post . excerpt . rendered } }
68
71
/>
72
+ < p >
73
+ < Link to = { `/blog/${ post . id } ` } > Read More ></ Link >
74
+ </ p >
69
75
</ div >
70
76
) ) }
71
77
</ div >
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import ErrorBoundary from './chapter13/ErrorBoundary';
8
8
import SyntaxHighlighter from 'react-syntax-highlighter' ;
9
9
import { github } from 'react-syntax-highlighter/dist/esm/styles/hljs' ;
10
10
import WpBlog from './WpBlog' ;
11
+ import WpBlogSP from './WpBlogSP' ;
11
12
const Introduction = lazy ( ( ) => import ( './Introduction' ) ) ;
12
13
const WhereToBuy = lazy ( ( ) => import ( './WhereToBuy' ) ) ;
13
14
const About = lazy ( ( ) => import ( './About' ) ) ;
@@ -34,9 +35,12 @@ export const routes = (
34
35
< Route path = "/intro" component = { Introduction } />
35
36
< Route path = "/WhereToBuy" component = { WhereToBuy } />
36
37
< Route path = "/AboutChrisMinnick" component = { About } />
37
- < Route path = "/blog" >
38
+ < Route exact path = "/blog/ " >
38
39
< WpBlog posts = { 10 } title = "ReactJS Foundations Blog and Errata" />
39
40
</ Route >
41
+ < Route exact path = "/blog/:id" >
42
+ < WpBlogSP title = "ReactJS Foundations Blog and Errata" />
43
+ </ Route >
40
44
< Route path = "/listing102" >
41
45
< Helmet >
42
46
< title >
You can’t perform that action at this time.
0 commit comments