77 * of patent rights can be found in the PATENTS file in the same directory.
88 */
99
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 ,
2814 hasMore : true ,
29- loadingMore : false ,
30- } ;
15+ loadingMore : false
16+ } ) ;
3117
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 ,
3523 page : nextPage ,
36- loadingMore : true ,
37- } ) ;
24+ loadingMore : true
25+ } ) ) ;
3826
39- var url = evt . target . href ;
40- var xhr = new XMLHttpRequest ( ) ;
27+ let url = '/comments/page-' + ( state . page + 1 ) ;
28+ let xhr = new XMLHttpRequest ( ) ;
4129 xhr . open ( 'GET' , url , true ) ;
4230 xhr . setRequestHeader ( 'Content-Type' , 'application/json' ) ;
31+
4332 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 ) ,
4737 hasMore : data . hasMore ,
48- loadingMore : false ,
49- } ) ;
38+ loadingMore : false
39+ } ) ) ;
5040 } ;
5141 xhr . send ( ) ;
5242 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- ) ;
6843 }
6944
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 ) {
7251 return < em > Loading...</ em > ;
73- } else if ( this . state . hasMore ) {
52+ } else if ( state . hasMore ) {
7453 return (
75- < a
76- href = { '/comments/page-' + ( this . state . page + 1 ) }
77- onClick = { this . loadMoreClicked }
78- >
54+ < Reactstrap . Button onClick = { loadMoreClicked } >
7955 Load More
80- </ a >
56+ </ Reactstrap . Button >
8157 ) ;
8258 } else {
8359 return < em > No more comments</ em > ;
8460 }
85- } ;
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+ ) ;
8671}
8772
8873class Comment extends React . Component {
8974 static propTypes = {
90- author : PropTypes . object . isRequired ,
75+ author : PropTypes . object . isRequired
9176 } ;
9277
9378 render ( ) {
@@ -104,7 +89,7 @@ class Comment extends React.Component {
10489
10590class Avatar extends React . Component {
10691 static propTypes = {
107- author : PropTypes . object . isRequired ,
92+ author : PropTypes . object . isRequired
10893 } ;
10994
11095 render ( ) {
@@ -114,7 +99,7 @@ class Avatar extends React.Component {
11499 alt = { 'Photo of ' + this . props . author . Name }
115100 width = { 50 }
116101 height = { 50 }
117- className = "commentPhoto"
102+ className = "commentPhoto mr-1 "
118103 />
119104 ) ;
120105 }
0 commit comments