Skip to content

Commit 36542d8

Browse files
committed
vite to part 1
1 parent 93bf390 commit 36542d8

File tree

17 files changed

+170
-82
lines changed

17 files changed

+170
-82
lines changed

src/content/1/en/part1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ lang: en
88

99
In this part, we will familiarize ourselves with the React-library, which we will be using to write the code that runs in the browser. We will also look at some features of JavaScript that are important for understanding React.
1010

11-
<i>Part updated 10th Jan 2023</i>
12-
- <i>No major changes</i>
11+
<i>Part updated 11th August 2023</i>
12+
- <i>Create React App replaced with Vite</i>
1313

1414
</div>

src/content/1/en/part1a.md

Lines changed: 82 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,36 @@ lang: en
99

1010
We will now start getting familiar with probably the most important topic of this course, namely the [React](https://react.dev/) library. Let's start by making a simple React application as well as getting to know the core concepts of React.
1111

12-
The easiest way to get started by far is by using a tool called [create-react-app](https://github.com/facebook/create-react-app). It is possible (but not necessary) to install <i>create-react-app</i> on your machine if the <i>npm</i> tool that was installed along with Node has a version number of at least <i>5.3</i>.
12+
The easiest way to get started by far is by using a tool called
13+
[Vite](https://vitejs.dev/).
1314

14-
> <i>You may also use the new generation frontend tool [Vite](https://vitejs.dev/) in this course if you wish. The create-react-app is still the tool recommended by the React team and that is why it remains the default tool to set up a React project in this course. Read [here](https://github.com/reactjs/reactjs.org/pull/5487#issuecomment-1409720741) how the React team sees the future of React tooling.</i>
15-
16-
Let's create an application called <i>part1</i> and navigate to its directory.
15+
Let's create an application called <i>part1</i>, navigate to its directory and install the libraries:
1716

1817
```bash
19-
npx create-react-app part1
18+
npm create vite@latest part -- --template react
2019
cd part1
20+
npm install
2121
```
2222

23-
The application runs as follows
23+
The application is started as follows
2424

2525
```bash
26-
npm start
26+
npm run dev
2727
```
2828

29-
By default, the application runs on localhost port 3000 with the address <http://localhost:3000>
29+
The console says that the application has started on localhost port 5173, i.e. the address <http://localhost:5173/>:
30+
31+
![](../../images/1/1-vite1.png)
32+
33+
Vite starts the application [by default](https://vitejs.dev/config/server-options.html#server-port) on port 5173. If it is not free, Vite uses the next free port number.
3034

31-
Your default browser should launch automatically. Open the browser console **immediately**. Also, open a text editor so that you can view the code as well as the webpage at the same time on the screen:
35+
Open the browser and a text editor so that you can view the code as well as the webpage at the same time on the screen:
3236

33-
![code and browser side by side](../../images/1/1e.png)
37+
![](../../images/1/1-vite4.png)
3438

35-
The code of the application resides in the <i>src</i> folder. Let's simplify the default code such that the contents of the file index.js looks like this:
39+
The code of the application resides in the <i>src</i> folder. Let's simplify the default code such that the contents of the file main.js looks like this:
3640

3741
```js
38-
import React from 'react'
3942
import ReactDOM from 'react-dom/client'
4043

4144
import App from './App'
@@ -46,40 +49,58 @@ ReactDOM.createRoot(document.getElementById('root')).render(<App />)
4649
and file <i>App.js</i> looks like this
4750

4851
```js
49-
const App = () => (
50-
<div>
51-
<p>Hello world</p>
52-
</div>
53-
)
52+
const App = () => {
53+
return (
54+
<div>
55+
<p>Hello world</p>
56+
</div>
57+
)
58+
}
5459

5560
export default App
5661
```
5762

58-
The files <i>App.css</i>, <i>App.test.js</i>, <i>index.css</i>, <i>logo.svg</i>, <i>setupTests.js</i> and <i>reportWebVitals.js</i> may be deleted as they are not needed in our application right now.
63+
The files <i>App.css</i> and <i>index.css</i>, and the directory <i>assets</i> may be deleted as they are not needed in our application right now.
64+
65+
### create-react-app
66+
67+
Instead od Vite you can also use the older generation tool [create-react-app](https://github.com/facebookincubator/create-react-app) in the course to set up the applications. The most visible difference to Vite is the name of the application startup file, which is <i>index.js</i>.
68+
69+
The way to start the application is also different, it is started with a command
70+
71+
```
72+
npm start
73+
```
74+
75+
The course is currently (11 August 2023) being updated to use Vite. Some brands may still use the application base created with create-react-app.
5976

6077
### Component
6178

62-
The file <i>App.js</i> now defines a [React component](https://react.dev/learn/your-first-component) with the name <i>App</i>. The command on the final line of file <i>index.js</i>
79+
The file <i>App.js</i> now defines a [React component](https://react.dev/learn/your-first-component) with the name <i>App</i>. The command on the final line of file <i>main.js</i>
6380

6481
```js
6582
ReactDOM.createRoot(document.getElementById('root')).render(<App />)
6683
```
6784

68-
renders its contents into the <i>div</i>-element, defined in the file <i>public/index.html</i>, having the <i>id</i> value 'root'.
85+
renders its contents into the <i>div</i>-element, defined in the file <i>index.html</i>, having the <i>id</i> value 'root'.
6986

70-
By default, the file <i>public/index.html</i> doesn't contain any HTML markup that is visible to us in the browser:
87+
By default, the file <i>index.html</i> doesn't contain any HTML markup that is visible to us in the browser:
7188

7289
```html
73-
<!DOCTYPE html>
90+
<!doctype html>
7491
<html lang="en">
7592
<head>
76-
content not shown ...
93+
<meta charset="UTF-8" />
94+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
95+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
96+
<title>Vite + React</title>
7797
</head>
7898
<body>
79-
<noscript>You need to enable JavaScript to run this app.</noscript>
8099
<div id="root"></div>
100+
<script type="module" src="/src/main.jsx"></script>
81101
</body>
82102
</html>
103+
83104
```
84105

85106
You can try adding there some HTML to the file. However, when using React, all content that needs to be rendered is usually defined as React components.
@@ -347,13 +368,47 @@ I really hope your console was open. If it was not, remember what you promised:
347368
348369
Software development is hard. It gets even harder if one is not using all the possible available tools such as the web-console and debug printing with _console.log_. Professionals use both <i>all the time</i> and there is no single reason why a beginner should not adopt the use of these wonderful helper methods that will make life so much easier.
349370

371+
### Possible error message
372+
373+
Depending on the editor you are using, you may receive the following error message at this point:
374+
375+
![](../../images/1/1-vite5.png)
376+
377+
It's not an actual error, but a warning caused by the [ESLint](https://eslint.org/) tool. You can silence the warning [react/prop-types](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/prop-types.md) by adding to the file <i>.eslintrc .cjs</i> the next line
378+
379+
```js
380+
module.exports = {
381+
root: true,
382+
env: { browser: true, es2020: true },
383+
extends: [
384+
'eslint:recommended',
385+
'plugin:react/recommended',
386+
'plugin:react/jsx-runtime',
387+
'plugin:react-hooks/recommended',
388+
],
389+
ignorePatterns: ['dist', '.eslintrc.cjs'],
390+
parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
391+
settings: { react: { version: '18.2' } },
392+
plugins: ['react-refresh'],
393+
rules: {
394+
'react-refresh/only-export-components': [
395+
'warn',
396+
{ allowConstantExport: true },
397+
],
398+
'react/prop-types': false // highlight-line
399+
},
400+
}
401+
```
402+
403+
We will get to know ESLint in more detail [in part 3](/osa3/validointi_ja_es_lint#lint).
404+
350405
### Some notes
351406

352407
React has been configured to generate quite clear error messages. Despite this, you should, at least in the beginning, advance in **very small steps** and make sure that every change works as desired.
353408

354409
**The console should always be open**. If the browser reports errors, it is not advisable to continue writing more code, hoping for miracles. You should instead try to understand the cause of the error and, for example, go back to the previous working state:
355410

356-
![screenshot of undefined prop error](../../images/1/2a.png)
411+
![screenshot of undefined prop error](../../images/1/1-vite6.png)
357412

358413
As we already mentioned, when programming with React, it is possible and worthwhile to write <em>console.log()</em> commands (which print to the console) within your code.
359414

@@ -399,7 +454,7 @@ const App = () => {
399454

400455
the result is an error message.
401456

402-
![multiple root elements error screenshot](../../images/1/3c.png)
457+
![multiple root elements error screenshot](../../images/1/1-vite7.png)
403458

404459
Using a root element is not the only working option. An <i>array</i> of components is also a valid solution:
405460

@@ -571,10 +626,9 @@ For each web application for a series of exercises, it is recommended to submit
571626

572627
<i>The application that we will start working on in this exercise will be further developed in a few of the following exercises. In this and other upcoming exercise sets in this course, it is enough to only submit the final state of the application. If desired, you may also create a commit for each exercise of the series, but this is entirely optional.</i>
573628

574-
Use create-react-app to initialize a new application. Modify <i>index.js</i> to match the following
629+
Use create-react-app to initialize a new application. Modify <i>main.js</i> to match the following
575630

576631
```js
577-
import React from 'react'
578632
import ReactDOM from 'react-dom/client'
579633

580634
import App from './App'
@@ -644,8 +698,6 @@ Careful, small-step progress may seem slow, but it is actually <i> by far the fa
644698
645699
that is, according to Martin, careful progress with small steps is even the only way to be fast.
646700

647-
**WARNING2** create-react-app automatically makes the project a git repository unless the application is created within an already existing repository. Most likely you **do not want** the project to become a repository, so run the command _rm -rf .git_ in the root of the project.
648-
649701
<h4>1.2: course information, step2</h4>
650702

651703
Refactor the <i>Content</i> component so that it does not render any names of parts or their number of exercises by itself. Instead, it only renders three <i>Part</i> components of which each renders the name and number of exercises of one part.

src/content/1/en/part1b.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ When it comes to syntax, the classes and the objects created from them are very
518518

519519
The introduction of the class syntax was a controversial addition. Check out [Not Awesome: ES6 Classes](https://github.com/petsel/not-awesome-es6-classes) or [Is “Class” In ES6 The New “Bad” Part? on Medium](https://medium.com/@rajaraodv/is-class-in-es6-the-new-bad-part-6c4e6fe1ee65) for more details.
520520

521-
The ES6 class syntax is used a lot in "old" React and also in Node.js, hence an understanding of it is beneficial even in this course. However, since we are using the new [Hooks](https://reactjs.org/docs/hooks-intro.html) feature of React throughout this course, we have no concrete use for JavaScript's class syntax.
521+
The ES6 class syntax is used a lot in "old" React and also in Node.js, hence an understanding of it is beneficial even in this course. However, since we are using the new [Hooks](https://react.dev/reference/react) feature of React throughout this course, we have no concrete use for JavaScript's class syntax.
522522

523523
### JavaScript materials
524524

src/content/1/en/part1c.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,9 @@ const App = (props) => {
192192
export default App
193193
```
194194
195-
And file <i>index.js</i> becomes:
195+
And file <i>main.js</i> becomes:
196196
197197
```js
198-
import React from 'react'
199198
import ReactDOM from 'react-dom/client'
200199

201200
import App from './App'
@@ -252,10 +251,9 @@ All of our components up till now have been simple in the sense that they have n
252251
253252
Next, let's add state to our application's <i>App</i> component with the help of React's [state hook](https://react.dev/learn/state-a-components-memory).
254253
255-
We will change the application as follows. <i>index.js</i> goes back to
254+
We will change the application as follows. <i>main.js</i> goes back to
256255
257256
```js
258-
import React from 'react'
259257
import ReactDOM from 'react-dom/client'
260258

261259
import App from './App'

src/content/1/en/part1d.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,8 +1141,6 @@ Remember, submit **all** the exercises of one part **in a single submission**. O
11411141

11421142
<i>Some of the exercises work on the same application. In these cases, it is sufficient to submit just the final version of the application. If you wish, you can make a commit after every finished exercise, but it is not mandatory.</i>
11431143

1144-
**WARNING** create-react-app will automatically turn your project into a git-repository unless you create your application inside of an existing git repository. **Most likely you do not want each of your projects to be a separate repository**, so simply run the _rm -rf .git_ command at the root of your application.
1145-
11461144
In some situations you may also have to run the command below from the root of the project:
11471145

11481146
```bash
@@ -1165,7 +1163,7 @@ The application must display the total number of collected feedback for each cat
11651163

11661164
Note that your application needs to work only during a single browser session. Once you refresh the page, the collected feedback is allowed to disappear.
11671165

1168-
It is advisable to use the same structure that is used in the material and previous exercise. File <i>index.js</i> is as follows:
1166+
It is advisable to use the same structure that is used in the material and previous exercise. File <i>main.js</i> is as follows:
11691167

11701168
```js
11711169
import React from 'react'
@@ -1312,16 +1310,14 @@ const App = () => {
13121310
export default App
13131311
```
13141312

1315-
Content of the file <i>index.js</i> is the same as in previous exercises.
1313+
Content of the file <i>main.js</i> is the same as in previous exercises.
13161314

13171315
Find out how to generate random numbers in JavaScript, eg. via a search engine or on [Mozilla Developer Network](https://developer.mozilla.org). Remember that you can test generating random numbers e.g. straight in the console of your browser.
13181316

13191317
Your finished application could look something like this:
13201318

13211319
![random anecdote with next button](../../images/1/18a.png)
13221320

1323-
**WARNING** create-react-app will automatically turn your project into a git-repository unless you create your application inside of an existing git repository. **Most likely you do not want each of your projects to be a separate repository**, so simply run the _rm -rf .git_ command at the root of your application.
1324-
13251321
<h4>1.13*: anecdotes step2</h4>
13261322

13271323
Expand your application so that you can vote for the displayed anecdote.

src/content/1/fi/osa1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ lang: fi
88

99
Alamme tässä osassa tutustua React-kirjastoon, jolla siis teemme sovelluksen selaimessa suoritettavan koodin. Teemme samalla myös nopean katsauksen Javascriptin Reactin kannalta oleellisimpiin ominaisuuksiin.
1010

11-
<i>Osa päivitetty 10.1.2023</i>
12-
- <i>Ei suuria muutoksia</i>
11+
<i>Osa päivitetty 11.8.2023</i>
12+
- <i>Create React App rkorvattu Vitellä</i>
1313

1414
</div>

0 commit comments

Comments
 (0)