Skip to content

Commit c3cfee8

Browse files
Update hooks demo in Mvc4 sample
1 parent 8b94109 commit c3cfee8

File tree

1 file changed

+37
-49
lines changed

1 file changed

+37
-49
lines changed

src/React.Sample.Mvc4/Content/Sample.jsx

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,79 +7,67 @@
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,
2915
loadingMore: false
30-
};
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,
3624
loadingMore: true
37-
});
25+
}));
3826

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();
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,
4838
loadingMore: false
49-
});
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-
<Reactstrap.Button onClick={this.loadMoreClicked}>
54+
<Reactstrap.Button onClick={loadMoreClicked}>
7655
Load More
7756
</Reactstrap.Button>
7857
);
7958
} else {
8059
return <em>No more comments</em>;
8160
}
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+
);
8371
}
8472

8573
class Comment extends React.Component {

0 commit comments

Comments
 (0)