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/5/en/part5b.md
+1-67Lines changed: 1 addition & 67 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -632,72 +632,6 @@ Show the button for deleting a blog post only if the blog post was added by the
632
632
633
633
<divclass="content">
634
634
635
-
### PropTypes
636
-
637
-
The <i>Togglable</i> component assumes that it is given the text for the button via the <i>buttonLabel</i> prop. If we forget to define it to the component:
638
-
639
-
```js
640
-
<Togglable> buttonLabel forgotten...</Togglable>
641
-
```
642
-
643
-
The application works, but the browser renders a button that has no label text.
644
-
645
-
We would like to enforce that when the <i>Togglable</i> component is used, the button label text prop must be given a value.
646
-
647
-
The expected and required props of a component can be defined with the [prop-types](https://github.com/facebook/prop-types) package. Let's install the package:
648
-
649
-
```shell
650
-
npm install prop-types
651
-
```
652
-
653
-
We can define the <i>buttonLabel</i> prop as a mandatory or <i>required</i> string-type prop as shown below:
654
-
655
-
```js
656
-
importPropTypesfrom'prop-types'
657
-
658
-
constTogglable=React.forwardRef((props, ref) => {
659
-
// ..
660
-
})
661
-
662
-
Togglable.propTypes= {
663
-
buttonLabel:PropTypes.string.isRequired
664
-
}
665
-
```
666
-
667
-
The console will display the following error message if the prop is left undefined:
668
-
669
-

670
-
671
-
The application still works and nothing forces us to define props despite the PropTypes definitions. Mind you, it is extremely unprofessional to leave <i>any</i> red output in the browser console.
672
-
673
-
Let's also define PropTypes to the <i>LoginForm</i> component:
674
-
675
-
```js
676
-
importPropTypesfrom'prop-types'
677
-
678
-
constLoginForm= ({
679
-
handleSubmit,
680
-
handleUsernameChange,
681
-
handlePasswordChange,
682
-
username,
683
-
password
684
-
}) => {
685
-
// ...
686
-
}
687
-
688
-
LoginForm.propTypes= {
689
-
handleSubmit:PropTypes.func.isRequired,
690
-
handleUsernameChange:PropTypes.func.isRequired,
691
-
handlePasswordChange:PropTypes.func.isRequired,
692
-
username:PropTypes.string.isRequired,
693
-
password:PropTypes.string.isRequired
694
-
}
695
-
```
696
-
697
-
If the type of a passed prop is wrong, e.g. if we try to define the <i>handleSubmit</i> prop as a string, then this will result in the following warning:
698
-
699
-

700
-
701
635
### ESlint
702
636
703
637
In part 3 we configured the [ESlint](/en/part3/validation_and_es_lint#lint) code style tool to the backend. Let's take ESlint to use in the frontend as well.
@@ -810,7 +744,7 @@ You can find the code for our current application in its entirety in the <i>part
810
744
811
745
#### 5.12: Blog List Frontend, step 12
812
746
813
-
Define PropTypes for one of the components of your application, and add ESlint to the project. Define the configuration according to your liking. Fix all of the linter errors.
747
+
Add ESlint to the project. Define the configuration according to your liking. Fix all of the linter errors.
814
748
815
749
Vite has installed ESlint to the project by default, so all that's left for you to do is define your desired configuration in the <i>.eslintrc.cjs</i> file.
Copy file name to clipboardExpand all lines: src/content/5/fi/osa5b.md
+1-67Lines changed: 1 addition & 67 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -630,72 +630,6 @@ Näytä poistonappi ainoastaan jos kyseessä on kirjautuneen käyttäjän lisä
630
630
631
631
<divclass="content">
632
632
633
-
### PropTypes
634
-
635
-
Komponentti <i>Togglable</i> olettaa, että sille määritellään propsina <i>buttonLabel</i> napin teksti. Jos määrittely unohtuu,
636
-
637
-
```js
638
-
<Togglable> buttonLabel unohtui...</Togglable>
639
-
```
640
-
641
-
sovellus kyllä toimii, mutta selaimeen renderöityy hämäävästi nappi, jolla ei ole mitään tekstiä.
642
-
643
-
Haluaisimmekin varmistaa, että jos <i>Togglable</i>-komponenttia käytetään, on propsille "pakko" antaa arvo.
644
-
645
-
Komponentin olettamat ja edellyttämät propsit ja niiden tyypit voidaan määritellä kirjaston [prop-types](https://github.com/facebook/prop-types) avulla. Asennetaan kirjasto:
646
-
647
-
```bash
648
-
npm install prop-types
649
-
```
650
-
651
-
<i>buttonLabel</i> voidaan määritellä <i>pakolliseksi</i> string-tyyppiseksi propsiksi seuraavasti:
652
-
653
-
```js
654
-
importPropTypesfrom'prop-types'
655
-
656
-
constTogglable=React.forwardRef((props, ref) => {
657
-
// ..
658
-
}
659
-
660
-
Togglable.propTypes= {
661
-
buttonLabel:PropTypes.string.isRequired
662
-
}
663
-
```
664
-
665
-
Jos propsia ei määritellä, seurauksena on konsoliin tulostuva virheilmoitus:
666
-
667
-

668
-
669
-
Koodi kuitenkin toimii edelleen, eli mikään ei pakota määrittelemään propseja PropTypes-määrittelyistä huolimatta. On kuitenkin erittäin epäammattimaista jättää konsoliin <i>mitään</i> punaisia tulosteita.
670
-
671
-
Määritellään Proptypet myös <i>LoginForm</i>-komponentille:
672
-
673
-
```js
674
-
importPropTypesfrom'prop-types'
675
-
676
-
constLoginForm= ({
677
-
handleSubmit,
678
-
handleUsernameChange,
679
-
handlePasswordChange,
680
-
username,
681
-
password
682
-
}) => {
683
-
// ...
684
-
}
685
-
686
-
LoginForm.propTypes= {
687
-
handleSubmit:PropTypes.func.isRequired,
688
-
handleUsernameChange:PropTypes.func.isRequired,
689
-
handlePasswordChange:PropTypes.func.isRequired,
690
-
username:PropTypes.string.isRequired,
691
-
password:PropTypes.string.isRequired
692
-
}
693
-
```
694
-
695
-
Jos propsin tyyppi on väärä, eli jos esimerkiksi yritetään määritellä propsiksi <i>handleSubmit</i> merkkijono, seurauksena on varoitus:
696
-
697
-

698
-
699
633
### ESLint
700
634
701
635
Konfiguroimme osassa 3 koodin tyylistä huolehtivan [ESLintin](/osa3/validointi_ja_es_lint) backendiin. Otetaan nyt ESLint käyttöön myös frontendissa.
@@ -808,7 +742,7 @@ Sovelluksen tämänhetkinen koodi on kokonaisuudessaan [GitHubissa](https://gith
808
742
809
743
#### 5.12: blogilistan frontend, step12
810
744
811
-
Määrittele joillekin sovelluksesi komponenteille PropTypet, ja ota projektiin käyttöön ESLint. Määrittele haluamasi kaltainen konfiguraatio. Korjaa kaikki lint-virheet.
745
+
Ota projektiin käyttöön ESLint. Määrittele haluamasi kaltainen konfiguraatio. Korjaa kaikki lint-virheet.
812
746
813
747
Vite on asentanut projektille ESLintin valmiiksi, joten ei tarvita muuta kun sopiva konfiguraatio tiedostoon <i>.eslintrc.cjs</i>.
0 commit comments