Today's objective is to apply the fundamentals of what we learned yesterday to build a Todo app in React.
Why build a todo app?
Because it is the prototypical CRUD app that will test your web development skills with a different technology in a familiar domain.
Ultimately, it is up to you how your application should function but the app should meet the specs for the following user stories:
Bronze
- As a user, I should be able to see all my incomplete todos
- As a user, I should be able to add a new todo to the list
- As a user, I should be able to mark a todo as completed
- As a user, I should able to edit a todo's content
- As a user, I should be able to delete a todo
Silver
- As a user, I should be able to also see a list of my completed todos
- As a user, when I mark a todo as complete, it should be added to my completed todos list
- As a user, when I mark a completed todo as incomplete, it should be added to my todos list
Gold
- As a user, I should be able to make a new list
- As a user, I should be able to make edit a list
- As a user, I should be able to delete a list.
Platinum
- As a user, when I refresh the page, I should be able to have my application's state be persisted
- As a user, I should be able to drag and drop todo items between lists.
Fork and clone this repo then run the necessary local setup:
$ git clone git@github.com:ga-wdi-exercises/todo-app-react.git
$ cd todo-app-react
$ npm i
$ npm start
If every thing was installed correctly, you should now be able to view starter application on http://localhost:3000
Initially, your project structure should look like this:
my-app/
README.md
index.html
favicon.ico
node_modules/
package.json
src/
App.css
App.js
index.css
index.js
logo.svg
For the project to build, these files must exist with exact filenames:
index.html
is the page template;favicon.ico
is the icon you see in the browser tab;src/index.js
is the JavaScript entry point.
You can delete or rename the other files.
You may create subdirectories inside src
. For faster rebuilds, only files inside src
are processed by Webpack.
You need to put any JS and CSS files inside src
, or Webpack won’t see them.
The generated project includes React and ReactDOM as dependencies. It also includes a set of scripts used by Create React App as a development dependency. You may install other dependencies (for example, React Router) with npm
:
npm install --save <library-name>
This project setup supports ES6 modules thanks to Babel.
While you can still use require()
and module.exports
, we encourage you to use import
and export
instead.
For example:
import React, { Component } from 'react';
class Button extends Component {
render() {
// ...
}
}
export default Button; // Don’t forget to use export default!
import React, { Component } from 'react';
import Button from './Button'; // Import a component from another file
class DangerButton extends Component {
render() {
return <Button color="red" />;
}
}
export default DangerButton;
Be aware of the difference between default and named exports. It is a common source of mistakes.
We suggest that you stick to using default imports and exports when a module only exports a single thing (for example, a component). That’s what you get when you use export default Button
and import Button from './Button'
.
Named exports are useful for utility modules that export several functions. A module may have at most one default export and as many named exports as you like.
Learn more about ES6 modules:
This project setup uses Webpack for handling all assets. Webpack offers a custom way of “extending” the concept of import
beyond JavaScript. To express that a JavaScript file depends on a CSS file, you need to import the CSS from the JavaScript file:
.Button {
padding: 20px;
}
import React, { Component } from 'react';
import './Button.css'; // Tell Webpack that Button.js uses these styles
class Button extends Component {
render() {
// You can use them as regular CSS styles
return <div className="Button" />;
}
}
This is not required for React but many people find this feature convenient. You can read about the benefits of this approach here. However you should be aware that this makes your code less portable to other build tools and environments than Webpack.
In development, expressing dependencies this way allows your styles to be reloaded on the fly as you edit them. In production, all CSS files will be concatenated into a single minified .css
file in the build output.
If you are concerned about using Webpack-specific semantics, you can put all your CSS right into src/index.css
. It would still be imported from src/index.js
, but you could always remove that import if you later migrate to a different build tool.
With Webpack, using static assets like images and fonts works similarly to CSS.
You can import
an image right in a JavaScript module. This tells Webpack to include that image in the bundle. Unlike CSS imports, importing an image or a font gives you a string value. This value is the final image path you can reference in your code.
Here is an example:
import React from 'react';
import logo from './logo.png'; // Tell Webpack this JS file uses this image
console.log(logo); // /logo.84287d09.png
function Header() {
// Import result is the URL of your image
return <img src={logo} alt="Logo" />;
}
export default function Header;
This works in CSS too:
.Logo {
background-image: url(./logo.png);
}
Webpack finds all relative module references in CSS (they start with ./
) and replaces them with the final paths from the compiled bundle. If you make a typo or accidentally delete an important file, you will see a compilation error, just like when you import a non-existent JavaScript module. The final filenames in the compiled bundle are generated by Webpack from content hashes. If the file content changes in the future, Webpack will give it a different name in production so you don’t need to worry about long-term caching of assets.
Please be advised that this is also a custom feature of Webpack.
It is not required for React but many people enjoy it (and React Native uses a similar mechanism for images). However it may not be portable to some other environments, such as Node.js and Browserify. If you prefer to reference static assets in a more traditional way outside the module system, please let us know in this issue, and we will consider support for this.
Note: this feature is available with
react-scripts@0.2.0
and higher.
First, open your package.json
and add a homepage
field.
It could look like this:
{
"name": "my-app",
"homepage": "http://myusername.github.io/my-app",
// ...
}
Now, whenever you run npm run build
, you will see a cheat sheet with a sequence of commands to deploy to GitHub pages:
git checkout -B gh-pages
git add -f build
git commit -am "Rebuild website"
git push origin :gh-pages
git subtree push --prefix build origin gh-pages
git checkout -