|
7 | 7 | * of patent rights can be found in the PATENTS file in the same directory.
|
8 | 8 | */
|
9 | 9 |
|
10 |
| -function HooksDemo() { |
11 |
| - let [count, updateCount] = React.useState(0); |
12 |
| - return ( |
13 |
| - <button onClick={() => updateCount(count + 1)}> |
14 |
| - Click count: {count} |
15 |
| - </button> |
16 |
| - ); |
17 |
| -} |
18 |
| - |
19 |
| -class CommentsBox extends React.Component { |
20 |
| - static propTypes = { |
21 |
| - initialComments: PropTypes.array.isRequired, |
22 |
| - page: PropTypes.number |
23 |
| - }; |
24 |
| - |
25 |
| - state = { |
26 |
| - comments: this.props.initialComments, |
27 |
| - page: this.props.page, |
| 10 | +function CommentsBox(props) { |
| 11 | + let [state, updateState] = React.useState({ |
| 12 | + comments: props.initialComments, |
| 13 | + page: props.page, |
28 | 14 | hasMore: true,
|
29 | 15 | loadingMore: false
|
30 |
| - }; |
| 16 | + }); |
31 | 17 |
|
32 |
| - loadMoreClicked = evt => { |
33 |
| - var nextPage = this.state.page + 1; |
34 |
| - this.setState({ |
| 18 | + function loadMoreClicked(evt) { |
| 19 | + let nextPage = state.page + 1; |
| 20 | + let comments = state.comments; |
| 21 | + updateState(prevState => ({ |
| 22 | + ...prevState, |
35 | 23 | page: nextPage,
|
36 | 24 | loadingMore: true
|
37 |
| - }); |
| 25 | + })); |
38 | 26 |
|
39 |
| - var url = '/comments/page-' + (this.state.page + 1); |
40 |
| - var xhr = new XMLHttpRequest(); |
| 27 | + let url = '/comments/page-' + (state.page + 1); |
| 28 | + let xhr = new XMLHttpRequest(); |
41 | 29 | xhr.open('GET', url, true);
|
42 | 30 | xhr.setRequestHeader('Content-Type', 'application/json');
|
| 31 | + |
43 | 32 | xhr.onload = () => {
|
44 |
| - var data = JSON.parse(xhr.responseText); |
45 |
| - this.setState({ |
46 |
| - comments: this.state.comments.concat(data.comments), |
| 33 | + let data = JSON.parse(xhr.responseText); |
| 34 | + updateState(prevState => ({ |
| 35 | + ...prevState, |
| 36 | + comments: comments.concat(data.comments), |
47 | 37 | hasMore: data.hasMore,
|
48 | 38 | loadingMore: false
|
49 |
| - }); |
| 39 | + })); |
50 | 40 | };
|
51 | 41 | xhr.send();
|
52 | 42 | evt.preventDefault();
|
53 |
| - }; |
54 |
| - |
55 |
| - render() { |
56 |
| - var commentNodes = this.state.comments.map(comment => ( |
57 |
| - <Comment author={comment.Author}>{comment.Text}</Comment> |
58 |
| - )); |
59 |
| - |
60 |
| - return ( |
61 |
| - <div className="comments"> |
62 |
| - <HooksDemo /> |
63 |
| - <h1>Comments</h1> |
64 |
| - <ol className="commentList">{commentNodes}</ol> |
65 |
| - {this.renderMoreLink()} |
66 |
| - </div> |
67 |
| - ); |
68 | 43 | }
|
69 | 44 |
|
70 |
| - renderMoreLink = () => { |
71 |
| - if (this.state.loadingMore) { |
| 45 | + let commentNodes = state.comments.map(comment => ( |
| 46 | + <Comment author={comment.Author}>{comment.Text}</Comment> |
| 47 | + )); |
| 48 | + |
| 49 | + function renderMoreLink() { |
| 50 | + if (state.loadingMore) { |
72 | 51 | return <em>Loading...</em>;
|
73 |
| - } else if (this.state.hasMore) { |
| 52 | + } else if (state.hasMore) { |
74 | 53 | return (
|
75 |
| - <Reactstrap.Button onClick={this.loadMoreClicked}> |
| 54 | + <Reactstrap.Button onClick={loadMoreClicked}> |
76 | 55 | Load More
|
77 | 56 | </Reactstrap.Button>
|
78 | 57 | );
|
79 | 58 | } else {
|
80 | 59 | return <em>No more comments</em>;
|
81 | 60 | }
|
82 |
| - }; |
| 61 | + } |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className="comments"> |
| 65 | + <h1>Comments</h1> |
| 66 | + <ol className="commentList">{commentNodes}</ol> |
| 67 | + {renderMoreLink()} |
| 68 | + <hr /> |
| 69 | + </div> |
| 70 | + ); |
83 | 71 | }
|
84 | 72 |
|
85 | 73 | class Comment extends React.Component {
|
|
0 commit comments