[WP] 21.03.2023 part2 | ref forwarding
fig 1.1 ref-forwarding scheme<https://dmitripavlutin.com/react-forwardref/> |
-----------------------------------------------------------------------
Ref forwarding… it is a technique for automatically passing a ref through a component to one of its children. It may be used especially in reusable component libraries. Example of application: focus input of custom component on page loaded.
- Create a ref within App component.
- pass a ref as a prop to custom component
- recieve ref within custom component as additional argument eg function CustomComponent({…}, ref)
- assign received ref as ref attribute in primitive input node.
- Edit export instruction – wrap it around React.forwardRef( ) function eg export default React.forwardRef(CustomComponent)
Komentarze
Prześlij komentarz