π Project Setup
To complete this challenge, you will need to start by setting it up:
β Create a new app by running the command:
Using npx
means that we avoid installing a local version of create-react-app
in our machine and instead we use the latest available version of create-react-app
in the npm
registry. It also completes a few other tasks for us:
- Creates a project directory for us (in this case
surreal-estate
) - Adds all tooling plus a
Hello World
React project - Installs our project dependencies (
node_modules
) automatically, so that we do not need to runnpm i
in our project directory
β Now that we have basic boilerplate installed in our computer we need to check this installation works correctly:
- Check the project starts by running one of these commands (stick to either
npm
oryarn
for all node modules commands):
- Check the test pass by running:
You might need to choose to A. Run all tests
in the Jest dialogue box.
β Then we need to update this boilerplate - this means removing all files, markup and styles that are irrelevant to our project. We suggest the following setup:
1. /public
folder
- delete all files except for
index.html
- update
index.html
to not use the favicon we just deleted and the manifest, deleted all comments and updated meta tags and page title
2. /src
folder
- Remove the
serviceWorker.js
,reportWebVitals.js
and SVG file. - Update the entry point to our application
index.js
by removing references to the service worker, reportWebVitals and comments. - Update
App.js
so that it does not use and render the SVG we just deleted.
π‘ If you run the application now with npm start
what do you see? The application should run correctly with no errors on the console and, as expected, the React logo is not being rendered. Can you see the favicon we have deleted though? If yes this will happen due to browser caching. If you open http://localhost:3000
in another browser you will see the favicon is not being rendered.
3. Tidy up the file structure in the /src
folder
At Manchester Codes we prefer a folder structure where we keep components, their styles and tests separate so we create three folders inside /src
:
- components
- tests
- styles
and then move App.js
, App.test.js
and App.css
to them correspondingly (App.css
should also be renamed to app.css
). The root of the /src
folder contains now only index.js
, index.css
and the setupTests.js
, which is a configuration file that powers our test suite.
π‘ The relative import paths in the files we've just moved will, most likely, need updating! An easy way to do this is to follow the Failed to compile errors presented when we run our application via npm start
or yarn start
.
###Β 4. Delete markup and styles for <App />
and create a simple <h2>
displaying the words Surreal Estate
.
In app.css
delete all styles.
5. Run your tests.
The only test in our test suite is failing. π€ Can you explain why this is happening?
π‘ Our test suite is looking for a piece of markup which contains the text "Learn React". This has been deleted when we changed the markup to "Surreal Estate".
βοΈ Try and re-write the test so that it looks for a piece of markup which contains the text that is now relevant.
6. Initialise a git repo.
Stage, commit and push your work. Don't forget to commit and push your work frequently and consistently with meaningful messages π.
7. Add a Google font
You can select as many styles as you want - choose the Embed
tab to have access to the instructions on how to add the font to a project. We encourage you to use the @import
method in index.css
instead of using a <script>
tag. Update the styling for <body>
element in your index.css
, replacing all the fonts brought in by create-react-app
boilerplate with the font of your choice. We used the Nunito font.
βοΈ Also delete the styling that is being applied to <code>
markup elements.
8. Add additional styling to the body
element
Set margin
and padding
to 0, and a light grey background.
9. Add the Airbnb style guide
For our project we will be using the Airbnb style guide, which in many quarters is considered the standard for developing React applications.
In any other project we would start by installing eslint
as one of of dev dependencies however, since we are using create-react-app
this dependency is already included. We do need to install a few other though:
We can do this by writing on the command line:
This will install a set of eslint
plugins that support Airbnb Style Guide. We also need to add a configuration file .eslintrc.json
to the root of your project with the following content:
The configuration above represents the standard Airbnb style guide, eg, this is the code style we would use if we were to be working in the Airbnb dev team. However there's a few contentious points like the enforcing of the strict use of .jsx
files for JSX (instead of being able to use either .jsx
or .js
(highly debated - have a look here).
In a nutshell, it goes like:
- React team: you have the freedom, β
go forth using
.js
files for JSX components! - Airbnb team: no β π π π»ββοΈ
At Manchester Codes we decided to side with freedom and the React team (after all they are the React team) and we're going to overwrite that rule and a couple more by editing the rules{}
key in the file .eslintrc
you just created (and saved, we hope) to:
Rules switched off [0]
Besides allowing us freedom to use .js
or .jsx
extensions, we are also allowing ourselves to use prop spreading like {...props}
and underscore dangles
as we will use a variable named _id
further on the track.
Rules set to Warning [1]
As you will be working with multiple imports in some files, this will highlight when they need to be re-ordered. The rules will also show you when you have declared a variable, function or state, and not used it, and when you have additional spaces or lines
Rules set to Error [2]
We are also enforcing the use of ES6 arrow functions for named components for consistency.
10. Install Prettier.
We strongly recommend the use of Prettier with VSCode as it helps with keeping to the style guide without much effort. If you do not have it installed in VSCode there is this great guide
If you already use Prettier with VSCode, just install a few dependencies with npm i -D prettier eslint-config-prettier eslint-plugin-prettier
and then update the extends
in the .eslintrc
file with:
You shouldn't see any linting errors but if you do try closing VSCode and opening it again so it's forced to read any changes to the configuration files.