@@ -20,6 +20,29 @@ class Post extends Component {
20
20
fetchPost
21
21
]
22
22
23
+ static propTypes = {
24
+ slug : PropTypes . string . isRequired ,
25
+ info : PropTypes . object . isRequired ,
26
+ singular : PropTypes . object . isRequired ,
27
+ dispatch : PropTypes . func . isRequired
28
+ }
29
+
30
+ constructor ( props ) {
31
+ super ( props )
32
+
33
+ /**
34
+ * State
35
+ *
36
+ * When `slug` exists, it means this component is rendered by the server.
37
+ *
38
+ * @type {Object }
39
+ * @see {@link https://github.com/reactjs/react-redux/issues/210 }
40
+ */
41
+ this . state = {
42
+ isWaitingForProps : ! props . singular . data . slug
43
+ }
44
+ }
45
+
23
46
/**
24
47
* Before mount
25
48
*
@@ -30,9 +53,15 @@ class Post extends Component {
30
53
* store doesn't match the requested page.
31
54
*/
32
55
componentWillMount ( ) {
33
- const { slug, data, isFetching, dispatch } = this . props
56
+ const { slug, singular, dispatch } = this . props
57
+ const { data, isFetching } = singular
58
+
59
+ if ( isFetching ) {
60
+ this . setState ( { isWaitingForProps : false } )
61
+ return
62
+ }
34
63
35
- if ( ! isFetching && slug !== data . slug ) {
64
+ if ( slug !== data . slug ) {
36
65
dispatch ( fetchPost ( { slug } ) )
37
66
}
38
67
}
@@ -46,11 +75,19 @@ class Post extends Component {
46
75
* @param {object } nextProps Next properties.
47
76
*/
48
77
componentWillReceiveProps ( nextProps ) {
49
- const { slug, isFetching, dispatch } = nextProps
78
+ const { slug, singular, dispatch } = nextProps
79
+ const { data, isFetching } = singular
50
80
51
- if ( ! isFetching && slug !== this . props . slug ) {
52
- dispatch ( fetchPost ( { slug } ) )
81
+ if ( isFetching ) {
82
+ this . setState ( { isWaitingForProps : false } )
83
+ return
84
+ }
85
+
86
+ if ( slug === this . props . slug ) {
87
+ return
53
88
}
89
+
90
+ dispatch ( fetchPost ( { slug } ) )
54
91
}
55
92
56
93
/**
@@ -59,10 +96,11 @@ class Post extends Component {
59
96
* TODO: Render meta, comments, etc.
60
97
*/
61
98
render ( ) {
62
- const { info, data, isFetching } = this . props
99
+ const { info, singular } = this . props
100
+ const { data, isFetching } = singular
63
101
let title
64
102
65
- if ( isFetching ) {
103
+ if ( isFetching || this . state . isWaitingForProps ) {
66
104
return ( < Spinner /> )
67
105
} else if ( ! data . id ) {
68
106
return ( < NotFound /> )
@@ -87,21 +125,13 @@ class Post extends Component {
87
125
}
88
126
}
89
127
90
- Post . propTypes = {
91
- slug : PropTypes . string . isRequired ,
92
- info : PropTypes . object . isRequired ,
93
- data : PropTypes . object ,
94
- isFetching : PropTypes . bool . isRequired ,
95
- dispatch : PropTypes . func . isRequired
96
- }
97
-
98
128
function mapStateToProps ( state , ownProps ) {
99
129
const { slug } = ownProps . params
100
130
101
131
return {
102
132
slug : slug ,
103
133
info : state . info ,
104
- ... state . singular
134
+ singular : state . singular
105
135
}
106
136
}
107
137
0 commit comments