@@ -87,6 +87,40 @@ class Content extends React.Component {
87
87
}
88
88
```
89
89
90
+ > Let's also update our ` ActivityItem ` component slightly to reflect our new ` activity ` object structure.
91
+ > We're also using [ Moment.js] ( https://momentjs.com/ ) library to format the dates into a human friendly string e.g ` 30 min ago `
92
+ > To include it in your file, add the following ` script ` tag to your document
93
+ > ``` html
94
+ > <script src =" https://unpkg.com/[email protected] /min/moment.min.js" ></script >
95
+ >```
96
+
97
+ ```javascript
98
+ class ActivityItem extends React.Component {
99
+ render() {
100
+ const { activity } = this.props;
101
+
102
+ return (
103
+ <div className =' item' >
104
+ <div className ={ ' avatar' } >
105
+ <img
106
+ alt =' avatar'
107
+ src ={activity.actor.avatar_url} />
108
+ </div >
109
+
110
+ <span className ={ ' time' } >
111
+ {moment(activity.created_at).fromNow()}
112
+ </span >
113
+
114
+ <p >{activity.actor.display_login} {activity.payload.action}</p >
115
+ <div className ={ ' right' } >
116
+ {activity.repo.name}
117
+ </div >
118
+ </div >
119
+ )
120
+ }
121
+ }
122
+ ```
123
+
90
124
Notice that we didn't change anything else from our ` Content ` component and it just works.
91
125
92
126
<div class =" demo " id =" fetchedTimeline " ></div >
@@ -171,7 +205,23 @@ Using this new `prop` (the `requestRefresh` prop), we can update the `activities
171
205
172
206
``` javascript
173
207
class Content extends React .Component {
174
- // ...
208
+ constructor {
209
+ this.state = {
210
+ activities: [],
211
+ loading: false // <~ set loading to false
212
+ };
213
+ }
214
+ // ...
215
+ updateData () {
216
+ this .setState (
217
+ {
218
+ loading: false ,
219
+ activities: data .sort (() => 0.5 - Math .random ()).slice (0 , 4 )
220
+ },
221
+ this .props .onComponentRefresh
222
+ );
223
+ }
224
+
175
225
componentWillReceiveProps (nextProps ) {
176
226
// Check to see if the requestRefresh prop has changed
177
227
if (nextProps .requestRefresh === true ) {
@@ -182,6 +232,18 @@ class Content extends React.Component {
182
232
}
183
233
```
184
234
235
+ Let's also update our ` componentWillMount ` method to call ` this.updateData() ` instead of ` this.setState `
236
+
237
+ ``` javascript
238
+ class Content extends React .Component {
239
+ // ...
240
+ componentDidMount () {
241
+ this .updateData ();
242
+ }
243
+ // ...
244
+ }
245
+ ```
246
+
185
247
<div class =" demo " id =" requestRefresh " ></div >
186
248
187
249
> This demo is using static data from a JSON file and randomly picking four elements when we refresh. This is set up to _ simulate_ a refresh.
0 commit comments