|
295 | 295 | |279| [How do you use contextType?](#how-do-you-use-contexttype)|
|
296 | 296 | |280| [What is a consumer?](#what-is-a-consumer)|
|
297 | 297 | |281| [How do you solve performance corner cases while using context?](#how-do-you-solve-performance-corner-cases-while-using-context)|
|
| 298 | +|282| [What is the purpose of forward ref in HOCs?](#what-is-the-purpose-of-forward-ref-in-hocs)| |
| 299 | +|283| [Is it ref argument available for all functions or class components?](#is-it-ref-argument-available-for-all-functions-or-class-components)| |
| 300 | +|284| [Why do you need additional care for component libraries while using forward refs?](#why-do-you-need-additional-care-for-component-libraries-while-using-forward-refs)| |
298 | 301 |
|
299 | 302 | ## Core React
|
300 | 303 |
|
|
4714 | 4717 | }
|
4715 | 4718 | }
|
4716 | 4719 | ```
|
| 4720 | +282. ### What is the purpose of forward ref in HOCs? |
| 4721 | + Refs will not get passed through because ref is not a prop. It handled differently by React just like **key**. If you add a ref to a HOC, the ref will refer to the outermost container component, not the wrapped component. In this case, you can use Forward Ref API. For example, we can explicitly forward refs to the inner FancyButton component using the React.forwardRef API. |
| 4722 | + The below HOC logs all props, |
| 4723 | + ```javascript |
| 4724 | + function logProps(Component) { |
| 4725 | + class LogProps extends React.Component { |
| 4726 | + componentDidUpdate(prevProps) { |
| 4727 | + console.log('old props:', prevProps); |
| 4728 | + console.log('new props:', this.props); |
| 4729 | + } |
| 4730 | + |
| 4731 | + render() { |
| 4732 | + const {forwardedRef, ...rest} = this.props; |
| 4733 | + |
| 4734 | + // Assign the custom prop "forwardedRef" as a ref |
| 4735 | + return <Component ref={forwardedRef} {...rest} />; |
| 4736 | + } |
| 4737 | + } |
| 4738 | + |
| 4739 | + return React.forwardRef((props, ref) => { |
| 4740 | + return <LogProps {...props} forwardedRef={ref} />; |
| 4741 | + }); |
| 4742 | + } |
| 4743 | + ``` |
| 4744 | + Let's use this HOC to log all props that get passed to our “fancy button” component, |
| 4745 | + ```javascript |
| 4746 | + class FancyButton extends React.Component { |
| 4747 | + focus() { |
| 4748 | + // ... |
| 4749 | + } |
| 4750 | + |
| 4751 | + // ... |
| 4752 | + } |
| 4753 | + export default logProps(FancyButton); |
| 4754 | + ``` |
| 4755 | + Now lets create a ref and pass it to FancyButton component. In this case, you can set focus to button element. |
| 4756 | + ```javascript |
| 4757 | + import FancyButton from './FancyButton'; |
| 4758 | + |
| 4759 | + const ref = React.createRef(); |
| 4760 | + ref.current.focus(); |
| 4761 | + <FancyButton |
| 4762 | + label="Click Me" |
| 4763 | + handleClick={handleClick} |
| 4764 | + ref={ref} |
| 4765 | + />; |
| 4766 | + ``` |
| 4767 | +283. ### Is it ref argument available for all functions or class components? |
| 4768 | + Regular function or class components don’t receive the ref argument, and ref is not available in props either. The second ref argument only exists when you define a component with React.forwardRef call. |
| 4769 | +284. ### Why do you need additional care for component libraries while using forward refs? |
| 4770 | + When you start using forwardRef in a component library, you should treat it as a breaking change and release a new major version of your library. This is because your library likely has a different behavior such as what refs get assigned to, and what types are exported. These changes can break apps and other libraries that depend on the old behavior. |
0 commit comments