Skip to content
This repository was archived by the owner on Oct 14, 2025. It is now read-only.

Commit 142ecd4

Browse files
committed
convert star to class component and fetch actual data from github api
1 parent 31c7fac commit 142ecd4

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

example/src/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default class App extends Component {
66
render () {
77
return (
88
<div>
9-
<Star repo={'vaibhavhrt/react-github-buttons'} />
9+
<Star owner='vaibhavhrt' repo='react-github-buttons' />
1010
</div>
1111
)
1212
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
},
2525
"dependencies": {},
2626
"peerDependencies": {
27+
"prop-types": "^15.7.2",
2728
"react": "^16.8.6",
2829
"react-dom": "^16.8.6"
2930
},

src/star/star.tsx

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,60 @@
11
/**
22
* @function Star
33
*/
4+
import * as PropTypes from "prop-types";
45
import * as React from "react";
6+
57
import classes from "../styles.css";
68
import StarSvg from "./starIcon";
79

8-
export interface IPropTypes { repo: string; }
10+
export interface IPropTypes {
11+
owner: string;
12+
repo: string;
13+
}
14+
15+
export interface IState {
16+
stargazers_count: number;
17+
}
18+
19+
export default class Star extends React.Component<IPropTypes, IState> {
20+
static propTypes = {
21+
owner: PropTypes.string.isRequired,
22+
repo: PropTypes.string.isRequired,
23+
};
24+
25+
constructor(props: IPropTypes) {
26+
super(props);
27+
this.state = {
28+
stargazers_count: 0,
29+
};
30+
}
31+
32+
componentDidMount() {
33+
const { owner, repo } = this.props;
34+
fetch(`https://api.github.com/repos/${owner}/${repo}`).then((res) => res.json()).then((res) => {
35+
this.setState({ stargazers_count: res.stargazers_count });
36+
});
37+
}
938

10-
export default function Star(props: IPropTypes) {
11-
const { repo } = props;
12-
return (
13-
<div className={classes.root}>
14-
<button title={`Star ${repo}`} className={classes.button}>
15-
<StarSvg />
16-
Star
17-
</button>
18-
<a
19-
className={classes.count}
20-
href={`https://github.com/${repo}/stargazers`}
21-
aria-label={`${1} user starred this repository`}
22-
target="_blank"
23-
rel="noreferrer noopener"
24-
>
25-
{1}
26-
</a>
27-
</div>
28-
);
39+
render() {
40+
const { owner, repo } = this.props;
41+
const { stargazers_count } = this.state;
42+
return (
43+
<div className={classes.root}>
44+
<button title={`Star ${owner}/${repo}`} className={classes.button}>
45+
<StarSvg />
46+
Star
47+
</button>
48+
<a
49+
className={classes.count}
50+
href={`https://github.com/${owner}/${repo}/stargazers`}
51+
aria-label={`${stargazers_count} user starred this repository`}
52+
target="_blank"
53+
rel="noreferrer noopener"
54+
>
55+
{stargazers_count}
56+
</a>
57+
</div>
58+
);
59+
}
2960
}

tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"tslint:recommended"
44
],
55
"rules": {
6-
"quotemark": [true, "double", "jsx-double"]
6+
"quotemark": [true, "double", "jsx-double"],
7+
"member-access": [true, "no-public"]
78
}
89
}

0 commit comments

Comments
 (0)