Skip to content

Commit dd55c44

Browse files
Update samples: Add hooks demo
1 parent b4a10af commit dd55c44

File tree

2 files changed

+47
-21
lines changed
  • src
    • React.Sample.Mvc4/Content
    • React.Sample.Webpack.CoreMvc/Content/components

2 files changed

+47
-21
lines changed

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

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,33 @@
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+
1019
class CommentsBox extends React.Component {
1120
static propTypes = {
1221
initialComments: PropTypes.array.isRequired,
13-
page: PropTypes.number
22+
page: PropTypes.number,
1423
};
1524

16-
state = {
17-
comments: this.props.initialComments,
18-
page: this.props.page,
19-
hasMore: true,
20-
loadingMore: false
25+
state = {
26+
comments: this.props.initialComments,
27+
page: this.props.page,
28+
hasMore: true,
29+
loadingMore: false,
2130
};
2231

23-
loadMoreClicked = (evt) => {
32+
loadMoreClicked = evt => {
2433
var nextPage = this.state.page + 1;
2534
this.setState({
2635
page: nextPage,
27-
loadingMore: true
36+
loadingMore: true,
2837
});
2938

3039
var url = evt.target.href;
@@ -36,24 +45,23 @@ class CommentsBox extends React.Component {
3645
this.setState({
3746
comments: this.state.comments.concat(data.comments),
3847
hasMore: data.hasMore,
39-
loadingMore: false
48+
loadingMore: false,
4049
});
4150
};
4251
xhr.send();
4352
evt.preventDefault();
4453
};
4554

4655
render() {
47-
var commentNodes = this.state.comments.map(comment =>
56+
var commentNodes = this.state.comments.map(comment => (
4857
<Comment author={comment.Author}>{comment.Text}</Comment>
49-
);
58+
));
5059

5160
return (
5261
<div className="comments">
62+
<HooksDemo />
5363
<h1>Comments</h1>
54-
<ol className="commentList">
55-
{commentNodes}
56-
</ol>
64+
<ol className="commentList">{commentNodes}</ol>
5765
{this.renderMoreLink()}
5866
</div>
5967
);
@@ -64,7 +72,10 @@ class CommentsBox extends React.Component {
6472
return <em>Loading...</em>;
6573
} else if (this.state.hasMore) {
6674
return (
67-
<a href={'/comments/page-' + (this.state.page + 1)} onClick={this.loadMoreClicked}>
75+
<a
76+
href={'/comments/page-' + (this.state.page + 1)}
77+
onClick={this.loadMoreClicked}
78+
>
6879
Load More
6980
</a>
7081
);
@@ -76,14 +87,15 @@ class CommentsBox extends React.Component {
7687

7788
class Comment extends React.Component {
7889
static propTypes = {
79-
author: PropTypes.object.isRequired
90+
author: PropTypes.object.isRequired,
8091
};
8192

8293
render() {
8394
return (
8495
<li>
8596
<Avatar author={this.props.author} />
86-
<strong>{this.props.author.Name}</strong>{': '}
97+
<strong>{this.props.author.Name}</strong>
98+
{': '}
8799
{this.props.children}
88100
</li>
89101
);
@@ -92,7 +104,7 @@ class Comment extends React.Component {
92104

93105
class Avatar extends React.Component {
94106
static propTypes = {
95-
author: PropTypes.object.isRequired
107+
author: PropTypes.object.isRequired,
96108
};
97109

98110
render() {
@@ -107,7 +119,11 @@ class Avatar extends React.Component {
107119
);
108120
}
109121

110-
getPhotoUrl = (author) => {
111-
return 'https://avatars.githubusercontent.com/' + author.GithubUsername + '?s=50';
122+
getPhotoUrl = author => {
123+
return (
124+
'https://avatars.githubusercontent.com/' +
125+
author.GithubUsername +
126+
'?s=50'
127+
);
112128
};
113129
}

src/React.Sample.Webpack.CoreMvc/Content/components/home.jsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Fragment } from 'react';
1+
import { Component, Fragment, useState } from 'react';
22
import {
33
Link,
44
BrowserRouter,
@@ -34,6 +34,15 @@ class Navbar extends Component {
3434
}
3535
}
3636

37+
function HooksDemo() {
38+
let [count, updateCount] = React.useState(0);
39+
return (
40+
<button onClick={() => updateCount(count + 1)}>
41+
Click count: {count}
42+
</button>
43+
);
44+
}
45+
3746
class HomePage extends Component {
3847
render() {
3948
return (
@@ -51,6 +60,7 @@ class HomePage extends Component {
5160
>
5261
ReactJS.NET is 🔥🔥
5362
</h1>
63+
<HooksDemo />
5464
</Fragment>
5565
);
5666
}

0 commit comments

Comments
 (0)