Skip to content

Commit 3cadd4a

Browse files
committed
Remove chapter about Proptypes as deprecated
1 parent 172446a commit 3cadd4a

File tree

2 files changed

+2
-134
lines changed

2 files changed

+2
-134
lines changed

src/content/5/en/part5b.md

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -632,72 +632,6 @@ Show the button for deleting a blog post only if the blog post was added by the
632632

633633
<div class="content">
634634

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-
import PropTypes from 'prop-types'
657-
658-
const Togglable = 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-
![console error stating buttonLabel is undefined](../../images/5/15.png)
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-
import PropTypes from 'prop-types'
677-
678-
const LoginForm = ({
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-
![console error saying handleSubmit expected a function](../../images/5/16.png)
700-
701635
### ESlint
702636

703637
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
810744

811745
#### 5.12: Blog List Frontend, step 12
812746

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.
814748

815749
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.
816750

src/content/5/fi/osa5b.md

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -630,72 +630,6 @@ Näytä poistonappi ainoastaan jos kyseessä on kirjautuneen käyttäjän lisä
630630

631631
<div class="content">
632632

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-
import PropTypes from 'prop-types'
655-
656-
const Togglable = 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-
![](../../images/5/15.png)
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-
import PropTypes from 'prop-types'
675-
676-
const LoginForm = ({
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-
![](../../images/5/16.png)
698-
699633
### ESLint
700634

701635
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
808742

809743
#### 5.12: blogilistan frontend, step12
810744

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.
812746

813747
Vite on asentanut projektille ESLintin valmiiksi, joten ei tarvita muuta kun sopiva konfiguraatio tiedostoon <i>.eslintrc.cjs</i>.
814748

0 commit comments

Comments
 (0)