You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/1/en/part1.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ lang: en
8
8
9
9
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.
Copy file name to clipboardExpand all lines: src/content/1/en/part1a.md
+82-30Lines changed: 82 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,33 +9,36 @@ lang: en
9
9
10
10
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.
11
11
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/).
13
14
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:
17
16
18
17
```bash
19
-
npx create-react-app part1
18
+
npm create vite@latest part -- --template react
20
19
cd part1
20
+
npm install
21
21
```
22
22
23
-
The application runs as follows
23
+
The application is started as follows
24
24
25
25
```bash
26
-
npm start
26
+
npm run dev
27
27
```
28
28
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
+

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.
30
34
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:
32
36
33
-

37
+

34
38
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:
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.
59
76
60
77
### Component
61
78
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>
<noscript>You need to enable JavaScript to run this app.</noscript>
80
99
<divid="root"></div>
100
+
<scripttype="module"src="/src/main.jsx"></script>
81
101
</body>
82
102
</html>
103
+
83
104
```
84
105
85
106
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:
347
368
348
369
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.
349
370
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
+

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
We will get to know ESLint in more detail [in part 3](/osa3/validointi_ja_es_lint#lint).
404
+
350
405
### Some notes
351
406
352
407
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.
353
408
354
409
**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:
355
410
356
-

411
+

357
412
358
413
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.
359
414
@@ -399,7 +454,7 @@ const App = () => {
399
454
400
455
the result is an error message.
401
456
402
-

457
+

403
458
404
459
Using a root element is not the only working option. An <i>array</i> of components is also a valid solution:
405
460
@@ -571,10 +626,9 @@ For each web application for a series of exercises, it is recommended to submit
571
626
572
627
<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>
573
628
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
575
630
576
631
```js
577
-
importReactfrom'react'
578
632
importReactDOMfrom'react-dom/client'
579
633
580
634
importAppfrom'./App'
@@ -644,8 +698,6 @@ Careful, small-step progress may seem slow, but it is actually <i> by far the fa
644
698
645
699
that is, according to Martin, careful progress with small steps is even the only way to be fast.
646
700
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
-
649
701
<h4>1.2: course information, step2</h4>
650
702
651
703
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.
Copy file name to clipboardExpand all lines: src/content/1/en/part1b.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -518,7 +518,7 @@ When it comes to syntax, the classes and the objects created from them are very
518
518
519
519
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.
520
520
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.
Copy file name to clipboardExpand all lines: src/content/1/en/part1c.md
+2-4Lines changed: 2 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -192,10 +192,9 @@ const App = (props) => {
192
192
exportdefaultApp
193
193
```
194
194
195
-
And file <i>index.js</i> becomes:
195
+
And file <i>main.js</i> becomes:
196
196
197
197
```js
198
-
importReactfrom'react'
199
198
importReactDOMfrom'react-dom/client'
200
199
201
200
importAppfrom'./App'
@@ -252,10 +251,9 @@ All of our components up till now have been simple in the sense that they have n
252
251
253
252
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).
254
253
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
Copy file name to clipboardExpand all lines: src/content/1/en/part1d.md
+2-6Lines changed: 2 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1141,8 +1141,6 @@ Remember, submit **all** the exercises of one part **in a single submission**. O
1141
1141
1142
1142
<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>
1143
1143
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
-
1146
1144
In some situations you may also have to run the command below from the root of the project:
1147
1145
1148
1146
```bash
@@ -1165,7 +1163,7 @@ The application must display the total number of collected feedback for each cat
1165
1163
1166
1164
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.
1167
1165
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:
1169
1167
1170
1168
```js
1171
1169
importReactfrom'react'
@@ -1312,16 +1310,14 @@ const App = () => {
1312
1310
exportdefaultApp
1313
1311
```
1314
1312
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.
1316
1314
1317
1315
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.
1318
1316
1319
1317
Your finished application could look something like this:
1320
1318
1321
1319

1322
1320
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
-
1325
1321
<h4>1.13*: anecdotes step2</h4>
1326
1322
1327
1323
Expand your application so that you can vote for the displayed anecdote.
Copy file name to clipboardExpand all lines: src/content/1/fi/osa1.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ lang: fi
8
8
9
9
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.
0 commit comments