diff --git a/src/content/5/en/part5b.md b/src/content/5/en/part5b.md index a80614fa06..da237753a4 100644 --- a/src/content/5/en/part5b.md +++ b/src/content/5/en/part5b.md @@ -388,9 +388,9 @@ The [useRef](https://react.dev/reference/react/useRef) hook is used to create a We also make the following changes to the Togglable component: ```js -import { useState, forwardRef, useImperativeHandle } from 'react' // highlight-line +import { useState, useImperativeHandle } from 'react' // highlight-line -const Togglable = forwardRef((props, refs) => { // highlight-line +const Togglable = (props) => { // highlight-line const [visible, setVisible] = useState(false) const hideWhenVisible = { display: visible ? 'none' : '' } @@ -401,7 +401,7 @@ const Togglable = forwardRef((props, refs) => { // highlight-line } // highlight-start - useImperativeHandle(refs, () => { + useImperativeHandle(props.ref, () => { return { toggleVisibility } @@ -419,12 +419,14 @@ const Togglable = forwardRef((props, refs) => { // highlight-line ) -}) // highlight-line +} // highlight-line export default Togglable ``` -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. +The reference assigned to the component can be accessed as a prop using `props.ref`. + +**NOTE:** Before React 19 the function that creates the component was supposed to be wrapped inside of a [forwardRef](https://react.dev/reference/react/forwardRef) function call to enable the component to access the ref assigned to it, that is no longer required, as it's now possible to access the ref as a prop. The component uses the [useImperativeHandle](https://react.dev/reference/react/useImperativeHandle) hook to make its toggleVisibility function available outside of the component.