Skip to content

Commit 8b06b95

Browse files
committed
Remove deprecated forwardRef
1 parent f88bada commit 8b06b95

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

src/content/5/en/part5b.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,9 @@ The [useRef](https://react.dev/reference/react/useRef) hook is used to create a
388388
We also make the following changes to the <i>Togglable</i> component:
389389

390390
```js
391-
import { useState, forwardRef, useImperativeHandle } from 'react' // highlight-line
391+
import { useState, useImperativeHandle } from 'react' // highlight-line
392392

393-
const Togglable = forwardRef((props, refs) => { // highlight-line
393+
const Togglable = (props) => { // highlight-line
394394
const [visible, setVisible] = useState(false)
395395

396396
const hideWhenVisible = { display: visible ? 'none' : '' }
@@ -401,10 +401,8 @@ const Togglable = forwardRef((props, refs) => { // highlight-line
401401
}
402402

403403
// highlight-start
404-
useImperativeHandle(refs, () => {
405-
return {
406-
toggleVisibility
407-
}
404+
useImperativeHandle(props.ref, () => {
405+
return { toggleVisibility }
408406
})
409407
// highlight-end
410408

@@ -419,13 +417,11 @@ const Togglable = forwardRef((props, refs) => { // highlight-line
419417
</div>
420418
</div>
421419
)
422-
}) // highlight-line
420+
}
423421

424422
export default Togglable
425423
```
426424

427-
The function that creates the component is wrapped inside of a [forwardRef](https://react.dev/reference/react/forwardRef) function call. This way the component can access the ref that is assigned to it.
428-
429425
The component uses the [useImperativeHandle](https://react.dev/reference/react/useImperativeHandle) hook to make its <i>toggleVisibility</i> function available outside of the component.
430426

431427
We can now hide the form by calling <i>noteFormRef.current.toggleVisibility()</i> after a new note has been created:

src/content/5/fi/osa5b.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ lang: fi
77

88
<div class="content">
99

10+
### Kirjautumislomakkeen näyttäminen vain tarvittaessa
11+
1012
Muutetaan sovellusta siten, että kirjautumislomaketta ei oletusarvoisesti näytetä:
1113

1214
![Oletusarvoisesti sovellus näytää ainoastaan muistiinpanojen listan sekä napin "log in"](../../images/5/10e.png)
@@ -388,9 +390,9 @@ const App = () => {
388390
Komponenttia <i>Togglable</i> laajennetaan seuraavasti
389391

390392
```js
391-
import { useState, useImperativeHandle, forwardRef } from 'react' // highlight-line
393+
import { useState, useImperativeHandle } from 'react' // highlight-line
392394

393-
const Togglable = forwardRef((props, ref) => { // highlight-line
395+
const Togglable = (props) => { // highlight-line
394396
const [visible, setVisible] = useState(false)
395397

396398
const hideWhenVisible = { display: visible ? 'none' : '' }
@@ -401,10 +403,8 @@ const Togglable = forwardRef((props, ref) => { // highlight-line
401403
}
402404

403405
// highlight-start
404-
useImperativeHandle(ref, () => {
405-
return {
406-
toggleVisibility
407-
}
406+
useImperativeHandle(props.ref, () => {
407+
return { toggleVisibility }
408408
})
409409
// highlight-end
410410

@@ -419,13 +419,11 @@ const Togglable = forwardRef((props, ref) => { // highlight-line
419419
</div>
420420
</div>
421421
)
422-
}) // highlight-line
422+
}
423423

424424
export default Togglable
425425
```
426426

427-
Komponentin luova funktio on kääritty funktiokutsun [forwardRef](https://react.dev/reference/react/forwardRef) sisälle, jolloin komponentti pääsee käsiksi sille määriteltyyn refiin.
428-
429427
Komponentti tarjoaa [useImperativeHandle](https://react.dev/reference/react/useImperativeHandle)-hookin avulla sisäisesti määritellyn funktionsa <i>toggleVisibility</i> ulkopuolelta kutsuttavaksi.
430428

431429
Voimme nyt piilottaa lomakkeen kutsumalla <i>noteFormRef.current.toggleVisibility()</i> samalla kun uuden muistiinpanon luominen tapahtuu:

0 commit comments

Comments
 (0)