Skip to content

Commit fba01f1

Browse files
committed
Replace useEffect with onCompleted
1 parent a1b280a commit fba01f1

File tree

2 files changed

+16
-50
lines changed

2 files changed

+16
-50
lines changed

src/content/8/en/part8b.md

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -896,46 +896,28 @@ the mutation response is <i>null</i>:
896896
897897
![dev tools showing network with localhost and response with editNumber being null](../../images/8/23ea.png)
898898
899-
For GraphQL, this is not an error, so registering an *onError* error handler is not useful.
900-
901-
We can use the *result* field returned by the *useMutation* hook as its second parameter to generate an error message.
899+
Since this isn’t considered an error state from GraphQL’s point of view, registering an _onError_ error handler wouldn’t be useful in this situation. However, we can add an _onCompleted_ callback to the _useMutation_ hook, where we can generate a potential error message:
902900
903901
```js
904-
import { useEffect, useState } from 'react' // highlight-line
905-
import { useMutation } from '@apollo/client/react'
906-
import { EDIT_NUMBER } from '../queries'
907-
908902
const PhoneForm = ({ setError }) => { // highlight-line
909903
const [name, setName] = useState('')
910904
const [phone, setPhone] = useState('')
911905

912-
const [ changeNumber, result ] = useMutation(EDIT_NUMBER) // highlight-line
913-
914-
const submit = (event) => {
915-
event.preventDefault()
916-
917-
changeNumber({ variables: { name, phone } })
918-
919-
setName('')
920-
setPhone('')
921-
}
922-
923906
// highlight-start
924-
useEffect(() => {
925-
if (result.data && result.data.editNumber === null) {
926-
setError('person not found')
907+
const [changeNumber] = useMutation(EDIT_NUMBER, {
908+
onCompleted: (data) => {
909+
if (!data.editNumber) {
910+
setError('person not found')
911+
}
927912
}
928-
929-
}, [result.data, setError])
913+
})
930914
// highlight-end
931915

932916
// ...
933917
}
934918
```
935919
936-
If a person cannot be found, or the *result.data.editNumber* is *null*, the component uses the callback function it received as props to set a suitable error message.
937-
We want to set the error message only when the result of the mutation
938-
*result.data* changes, so we use the useEffect hook to control setting the error message.
920+
The _onCompleted_ callback function is always executed when the mutation has been successfully completed. If the person wasn’t found—that is, if the query result _data.editNumber_ is _null_—the component uses the _setError_ callback function it received via props to set an appropriate error message.
939921
940922
The current code of the application can be found on [GitHub](https://github.com/fullstack-hy2020/graphql-phonebook-frontend/tree/part8-4) branch <i>part8-4</i>.
941923

src/content/8/fi/osa8b.md

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -880,44 +880,28 @@ Sovelluksessa on vielä pieni ongelma. Jos yritämme vaihtaa olemattomaan nimee
880880

881881
![](../../images/8/23ea.png)
882882

883-
Koska kyseessä ei ole GraphQL:n kannalta virhetilanne, ei _onError_-virheenkäsittelijän rekisteröimisestä olisi tässä tilanteessa hyötyä.
884-
885-
Voimme generoida virheilmoituksen _useMutation_-hookin toisena parametrina palauttaman mutaation tuloksen kertovan olion _result_ avulla.
883+
Koska kyseessä ei ole GraphQL:n kannalta virhetilanne, ei _onError_-virheenkäsittelijän rekisteröimisestä olisi tässä tilanteessa hyötyä. Voimme kuitenkin lisätä _useMutation_-hookille _onCompleted_-takaisinkutsufunktion, jossa mahdollinen virheilmoitus voidaan generoida:
886884

887885
```js
888-
import { useEffect, useState } from 'react' // highlight-line
889-
import { useMutation } from '@apollo/client/react'
890-
import { EDIT_NUMBER } from '../queries'
891-
892886
const PhoneForm = ({ setError }) => { // highlight-line
893887
const [name, setName] = useState('')
894888
const [phone, setPhone] = useState('')
895889
896-
const [ changeNumber, result ] = useMutation(EDIT_NUMBER) // highlight-line
897-
898-
const submit = (event) => {
899-
event.preventDefault()
900-
901-
changeNumber({ variables: { name, phone } })
902-
903-
setName('')
904-
setPhone('')
905-
}
906-
907890
// highlight-start
908-
useEffect(() => {
909-
if (result.data && result.data.editNumber === null) {
910-
setError('person not found')
891+
const [changeNumber] = useMutation(EDIT_NUMBER, {
892+
onCompleted: (data) => {
893+
if (!data.editNumber) {
894+
setError('person not found')
895+
}
911896
}
912-
913-
}, [result.data, setError])
897+
})
914898
// highlight-end
915899
916900
// ...
917901
}
918902
```
919903

920-
Jos henkilöä ei löytynyt, eli kyselyn tulos _result.data.editNumber_ on _null_, asettaa komponentti propseina saamansa callback-funktion avulla sopivan virheilmoituksen. Virheilmoituksen asettamista kontrolloidaan useEffect-hookin avulla, ja hook suoritetaan aina kun mutaation tulos _result.data_ muuttuu.
904+
Takaisinkutsufunktio _onCompleted_ suoritetaan aina, kun mutaatio on onnistuneesti suoritettu. Jos henkilöä ei löytynyt, eli kyselyn tulos _data.editNumber_ on _null_, asettaa komponentti propseina saamansa callback-funktion _setError_ avulla sopivan virheilmoituksen.
921905

922906
Sovelluksen tämänhetkinen koodi on [GitHubissa](https://github.com/fullstack-hy2020/graphql-phonebook-frontend/tree/part8-4), branchissa <i>part8-4</i>.
923907

0 commit comments

Comments
 (0)