Skip to content

Commit 398c115

Browse files
committed
Changed for create-react-app
1 parent ca7353a commit 398c115

File tree

14 files changed

+168
-170
lines changed

14 files changed

+168
-170
lines changed

.babelrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

.gitignore

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1-
/src/js/app-bundle.js*
2-
node_modules/
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
6+
# testing
7+
/coverage
8+
9+
# production
10+
/build
11+
12+
# misc
13+
.DS_Store
14+
.env
15+
npm-debug.log*
16+
yarn-debug.log*
17+
yarn-error.log*
18+

README.md

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,20 @@ The code you are given for the project implements the search form and the loadin
1010

1111
Let's take a look at the code that's already there. Many of the starter files should already be familiar to you if you completed [the previous workshop](https://github.com/ziad-saab/react-intro-workshop).
1212

13-
* `server.js`: Tiny Express app that will serve our application files
14-
* `webpack.config.js`: Configuration for bundling our code files to `app-bundle.js`
1513
* `package.json`: Configuration file for NPM, contains dependencies and project metadata
16-
* `.gitignore`: Files that should be ignored by Git. `node_modules` can always be regenerated, and so can `app-bundle.js`.
17-
* `src/index.html`: File that gets served by Express to load our app
18-
* `src/js/app.js`: This file is the entry point for Webpack. It puts our app on the screen.
19-
* `src/js/app-bundle.js`: Never need to touch this file. It gets generated by Webpack and is loaded by the browser.
20-
* `src/js/app-bundle.js.map`: Contains source mapping info for the browser's Developer Tools
21-
* `src/js/components/*`: All the components of our application.
22-
* `src/css/app.css`: The styles for our app. Check it out to see how your starter app is being styled, and add to it to complete the project.
14+
* `.gitignore`: Files that should be ignored by Git. `node_modules` can always be regenerated
15+
* `public/index.html`: File that gets served thru Webpack after having been filled in
16+
* `src/index.js`: This file is the entry point for the app. It puts our app on the screen!
17+
* `src/components/*`: All the components of our application.
18+
* `src/index.css`: The styles for our app. Check it out to see how your starter app is being styled, and add to it to complete the project. Notice that we don't <link> this CSS from the index? How does this work?? Make sure you understand!!!
2319

2420
**To get started coding on this project, remember the following steps**:
2521

2622
1. `npm install` the first time you clone this repo
27-
2. `npm run dev` anytime you want to start developing. This will watch your JS files and re-run webpack when there are changes
28-
3. `node server.js` to start the Express server that will serve the app.
23+
2. `npm start` anytime you want to start developing. This will watch your JS files and re-run webpack when there are changes
24+
3. Start coding!
2925

30-
In `app.js` we have the following route structure:
26+
In `index.js` we have the following route structure:
3127

3228
```javascript
3329
<Route path="/" component={App}>
@@ -94,7 +90,7 @@ When clicking on the followers link in the UI, notice that the URL changes to `/
9490
![followers page](http://i.imgur.com/IwkBOUc.png)
9591

9692
### Step 1: adding the route
97-
In `app.js`, you currently have your user route setup like this:
93+
In `index.js`, you currently have your user route setup like this:
9894

9995
```javascript
10096
<Route path="user/:username" component={User} />
@@ -122,7 +118,7 @@ For the moment, create the component only with a `render` function. In there, us
122118
```
123119

124120
### Step 3: displaying the nested component inside its parent
125-
When the URL changes to `followers`, we want to display the followers alongside the current `User` component. This is why we are nesting the followers route inside user.
121+
When the URL changes to `followers`, we want to display the followers alongside the current `User` component. **This is why we are nesting the followers route inside the user route.**
126122

127123
To reflect this nesting in our tree of components, we have to add a `{this.props.children}` output to our `User` component.
128124

@@ -131,7 +127,7 @@ Modify the `User` component to make it display its children just before the clos
131127
When this is done, go back to your browser. Search for a user, and click on FOLLOWERS. The followers component should be displayed below the user info.
132128

133129
### Step 4: loading GitHub data in the `Followers` component:
134-
We want to load the followers of the current user as soon as the `Followers` component is mounted in the DOM. In the `componentDidMount` of `Followers`, use jQuery's `getJSON` to make a request to GitHub's API for the followers. Simply add `/followers` to the GitHub API URL for the user e.g. https://api.github.com/users/ziad-saab/followers
130+
We want to load the followers of the current user as soon as the `Followers` component is mounted in the DOM. In the `componentDidMount` of `Followers`, use `fetch` to make a request to GitHub's API for the followers. Simply add `/followers` to the GitHub API URL for the user e.g. https://api.github.com/users/ziad-saab/followers
135131

136132
In the callback to your AJAX request, use `setState` to set a `followers` state on your component.
137133

@@ -147,28 +143,26 @@ Using the `this.state.followers` in your `render` method, display the followers
147143
</Link>
148144
```
149145

150-
And here's a visual example of four `GithubUser` instances (remember to use `vertical-align` in your CSS to align the image and the name):
146+
And here's a visual example of four `GithubUser` instances (you can use `vertical-align` in your CSS to align the image and the name):
151147

152148
![GithubUser component](http://i.imgur.com/dWp7NIc.png)
153149

154-
2. In `Followers`, use require to import your `GithubUser` component.
150+
2. In `Followers`, import your `GithubUser` component.
155151
3. In the `render` method of `Followers`, use `map` to take the array at `this.state.followers`, and map it to an array of `<GithubUser />` elements, passing the `user` prop. The code of `Followers`' `render` method should look like this:
156152

157153
```javascript
158-
function render() {
159-
if (!this.state.followers) {
160-
return <div>LOADING FOLLOWERS...</div>
161-
}
162-
163-
return (
164-
<div className="followers-page">
165-
<h2>Followers of {this.props.params.username}</h2>
166-
<ul>
167-
{this.state.followers.map(/* INSERT CODE HERE TO RETURN A NEW <GithubUser/> */)}
168-
</ul>
169-
</div>
170-
);
154+
if (!this.state.followers) {
155+
return <div>LOADING FOLLOWERS...</div>
171156
}
157+
158+
return (
159+
<div className="followers-page">
160+
<h2>Followers of {this.props.params.username}</h2>
161+
<ul>
162+
{this.state.followers.map(/* INSERT CODE HERE TO RETURN A NEW <GithubUser/> */)}
163+
</ul>
164+
</div>
165+
);
172166
```
173167

174168
Having done this, you should have a full `Followers` component ready to go.
@@ -224,7 +218,7 @@ For this challenge, we're going to use the [`react-infinite`](https://github.com
224218

225219
Right now, if you look at a profile with a lot of followers, you'll notice that GitHub API only returns the first 25 followers. The API has a `per_page` query string parameter that you can set, but the maximum number of items per page is still 100. If someone has more than 100 followers, you'd have to do many requests to get all of them.
226220

227-
React Infinite will take care of most of the heavy lifting for us. First of all, it's never a good idea to have thousands of elements on the page if the user is only seeing a handful. React Infinite will be efficient in showing only the elements that are in the view. Second, React Infinite will detect the scroll position and can fire off a callback when the scrolling reaches the edge of your container.
221+
React Infinite will take care of most of the heavy lifting for us. First of all, it's never a good idea to have thousands of elements on the page if the user is only seeing a handful. React Infinite will be efficient in showing only the elements that are in the viewport. Second, React Infinite will detect the scroll position and can fire off a callback when the scrolling reaches the edge of your container.
228222

229223
All you have to do is provide React Infinite with the callback function that will load your data, and pass your items to the `<Infinite>` component.
230224

@@ -239,11 +233,13 @@ Let's do it step by step for **followers** and then you can reproduce it for the
239233
Your `Followers` component currently loads its data on `componentDidMount`. It turns out that if you mount an `<Infinite>` component without any data, it will automatically call your callback function to fetch more data.
240234

241235
### Step 1.1: Adding new state data to `Followers`
242-
In the `getInitialState` method of `Followers`, let's add a few more pieces of state. Let's add a `page` state and initialize it to `1`. Add another state called `loading` and set it to `false`. Finally, add a `followers` state and set it to an empty array.
236+
In the `constructor` method of `Followers`, let's add a few more pieces of state. Let's add a `page` state and initialize it to `1`. Add another state called `loading` and set it to `false`. Finally, add a `followers` state and set it to an empty array.
243237

244238
### Step 1.2: Change the `componentDidMount` method name to `fetchData`. In your AJAX call, add two query string parameters to the GitHub API URL: `page` will come from your state, and `per_page` can be set to anything between 1 and 100. Set it to 50. Your URL should look like this:
245239

246-
`https://api.github.com/users/USER/followers?access_token=XXX&page=1&per_page=50`
240+
```
241+
https://api.github.com/users/USER/followers?access_token=XXX&page=1&per_page=50
242+
```
247243

248244
### Step 1.3: Loading...
249245
Before doing the AJAX call in `fetchData`, set the `loading` state to `true`.

package.json

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
{
2-
"name": "react-github-project",
3-
"version": "1.0.0",
4-
"description": "Create a GitHub browser using React and the GitHub API",
5-
"main": "server.js",
6-
"keywords": ["react","github"],
7-
"author": "Ziad Saab <ziad.saab@gmail.com>",
8-
"license": "MIT",
9-
"scripts": {
10-
"dev": "webpack --watch"
2+
"name": "bleh",
3+
"version": "0.1.0",
4+
"private": true,
5+
"devDependencies": {
6+
"react-scripts": "0.9.0"
117
},
128
"dependencies": {
13-
"babel-core": "^6.13.2",
14-
"babel-loader": "^6.2.4",
15-
"babel-preset-es2015": "^6.13.2",
16-
"babel-preset-react": "^6.11.1",
17-
"express": "^4.14.0",
18-
"jquery": "^3.1.0",
19-
"react": "^15.3.0",
20-
"react-dom": "^15.3.0",
21-
"react-router": "^2.6.1",
22-
"webpack": "^1.13.1"
9+
"react": "^15.4.2",
10+
"react-dom": "^15.4.2",
11+
"react-router": "^3.0.2"
12+
},
13+
"scripts": {
14+
"start": "react-scripts start",
15+
"build": "react-scripts build",
16+
"test": "react-scripts test --env=jsdom",
17+
"eject": "react-scripts eject"
2318
}
2419
}

public/favicon.ico

24.3 KB
Binary file not shown.

public/index.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
7+
<!--
8+
Notice the use of %PUBLIC_URL% in the tag above.
9+
It will be replaced with the URL of the `public` folder during the build.
10+
Only files inside the `public` folder can be referenced from the HTML.
11+
12+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
13+
work correctly both with client-side routing and a non-root public URL.
14+
Learn how to configure a non-root public URL by running `npm run build`.
15+
-->
16+
<title>GitHub API Project</title>
17+
</head>
18+
<body>
19+
<div id="root"></div>
20+
<!--
21+
This HTML file is a template.
22+
If you open it directly in the browser, you will see an empty page.
23+
24+
You can add webfonts, meta tags, or analytics to this file.
25+
The build step will place the bundled scripts into the <body> tag.
26+
27+
To begin the development, run `npm start`.
28+
To create a production bundle, use `npm run build`.
29+
-->
30+
</body>
31+
</html>

server.js

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var React = require('react');
2-
var Link = require('react-router').Link;
1+
import React from 'react';
2+
import { Link } from 'react-router';
33

44
/*
55
This is the layout component. It's displayed by the top-level Route
@@ -8,8 +8,8 @@ this.props.children will correspond to the current URL's component.
88
If the URL is only / then the IndexRoute's component will be the child (Search component)
99
If the URL is /user/:username then the User component will be displayed.
1010
*/
11-
var App = React.createClass({
12-
render: function() {
11+
class App extends React.Component {
12+
render() {
1313
return (
1414
<div className="main-app">
1515
<header className="main-header">
@@ -21,6 +21,6 @@ var App = React.createClass({
2121
</div>
2222
);
2323
}
24-
});
24+
};
2525

26-
module.exports = App;
26+
export default App;
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
var React = require('react');
2-
var history = require('react-router').browserHistory;
1+
import React from 'react';
2+
import { browserHistory as history } from 'react-router';
33

44
/*
55
This component displays a form where the user can enter a GitHub username
66
When they submit the form either by pressing ENTER or clicking the button,
77
we will use react-router's history.push function to push a new URL to the history.
88
99
This will have as an effect to navigate to a new URL, which will display the User component
10+
Why are we doing this instead of using a <Link>? The answer is straightforward, but make sure you understand!!!
1011
*/
11-
var Search = React.createClass({
12-
_handleSubmit: function(e) {
12+
class Search extends React.Component {
13+
constructor(props) {
14+
super(props);
15+
16+
// Why do we need to do this?? Make sure you understand!!!
17+
this._handleSubmit = this._handleSubmit.bind(this);
18+
}
19+
_handleSubmit(e) {
1320
e.preventDefault();
1421
history.push(`/user/${this.refs.userInput.value}`)
15-
},
16-
render: function() {
22+
}
23+
24+
render() {
1725
return (
1826
<div className="search-page">
1927
<h2>Enter a GitHub username</h2>
@@ -24,6 +32,6 @@ var Search = React.createClass({
2432
</div>
2533
);
2634
}
27-
});
35+
};
2836

29-
module.exports = Search;
37+
export default Search;

0 commit comments

Comments
 (0)