how to deploy react app on github code example
Example 1: how to install gh-pages
npm install gh-pages --save-dev
Example 2: upload react project to github
Make sure you have already created a repo in github.
In "package.json" file of project add - "homepage": "https://username.github.io/reponame"
Run Install - "yarn add gh-pages"
In "package.json/scripts" add `"predeploy":"npm run build", "deploy":"gh-pages -d build"`
Run "npm run build" to create the build
To push in git use following steps:
1) git init
2) git add -A
3) git commmit -m "first commit"
4) git remote add origin https://github.com/username/reponame.git
5) git push -u origin master
Now in github repo create a new branch "gh-pages"
In settings -> github pages -> source -> branch -> gh-pages -> save
Run "npm run deploy"
*****************************voilà it's done!!*************************
Example 3: how to make a github api using react
class GitHubSearch extends React.Component {
constructor(props){
super(props);
this.state = {
username: '',
userrepo: '',
};
}
getUser(username) {
return fetch(`https://api.github.com/users/${username}`)
.then(response => response.json())
.then(response => {
return response;
})
}
getUserRepo(userrepo) {
return fetch(`https://api.github.com/users/${username}/repos`)
.then(response => response.json())
.then(response => {
return response;
})
}
async handleSubmit(e) {
e.preventDefault();
let user = await this.getUser(this.refs.username.value);
this.setState({ avatar_url: user.avatar_url,
username: user.login,
followers: user.followers,
following: user.following,
url: user.url,
});
let repo = await this.getUserRepo(this.refs.userrepo.value);
this.setState({ name: repo.name,
description: repo.description,
git_url: repo.git_url,
stargazers_count: repo.stargazers_count,
forks_count: repo.forks_count,
open_issues_count: repo.open_issues_count,
size: repo.size,
})
}
render() {
let user;
if(this.state.username) {
user =
<div className="resultBadge">
<img src={this.state.avatar_url}/>
<p className="userInfo">
Username: <br/>
{this.state.username}
</p>
<p className="followerInfo">
{this.state.followers} Followers
</p>
<p className="followingInfo">
Following {this.state.following} users
</p>
</div>
}
let repo;
if(this.state.userrepo) {
repo =
<div className="repoResults">
<p>
{this.state.name}
</p>
</div>
}
return (
<div className="GitHubSearch">
<header className="Search-header">
<h1>Github User Search </h1>
</header>
<form onSubmit={e => this.handleSubmit(e)}>
<input ref='username' type='text' placeholder='username' />
</form>
<p className="Search-intro">
{user}
</p>
<p>
{repo}
</p>
</div>
);
}
}
ReactDOM.render(<GitHubSearch/>, document.getElementById('container'));
Example 4: create react app github repo
$ git remote add origin https://github.com/gitname/react-gh-pages.git
Example 5: deploy react express to github pages
npm run deploy
Example 6: deploy react app to github
echo "# instagram-clone" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/CoderJakaria/instagram-clone.git
git push -u origin main