File tree Expand file tree Collapse file tree
examples/16-3-release-blog-post Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11function withTheme ( Component ) {
2- // Note the second param "ref" provided by React.forwardRef.
3- // We can attach this to Component directly.
4- // highlight-range{1,5}
5- function ThemedComponent ( props , ref ) {
2+ // highlight-next-line
3+ function ThemedComponent ( { forwardedRef, ...rest } ) {
64 return (
75 < ThemeContext . Consumer >
86 { theme => (
9- < Component { ...props } ref = { ref } theme = { theme } />
7+ // Assign the custom prop "forwardedRef" as a ref
8+ // highlight-next-line
9+ < Component
10+ { ...rest }
11+ ref = { forwardedRef }
12+ theme = { theme }
13+ />
1014 ) }
1115 </ ThemeContext . Consumer >
1216 ) ;
1317 }
1418
15- // These next lines are not necessary,
16- // But they do give the component a better display name in DevTools,
17- // e.g. "ForwardRef(withTheme(MyComponent))"
18- // highlight-range{1-2}
19- const name = Component . displayName || Component . name ;
20- ThemedComponent . displayName = `withTheme(${ name } )` ;
21-
22- // Tell React to pass the "ref" to ThemedComponent.
23- // highlight-next-line
24- return React . forwardRef ( ThemedComponent ) ;
19+ // Note the second param "ref" provided by React.forwardRef.
20+ // We can pass it along to ThemedComponent as a regular prop, e.g. "forwardedRef"
21+ // And it can then be attached to the Component.
22+ // highlight-range{1-3}
23+ return React . forwardRef ( ( props , ref ) => (
24+ < ThemedComponent { ...props } forwardedRef = { ref } />
25+ ) ) ;
2526}
2627
2728// highlight-next-line
You can’t perform that action at this time.
0 commit comments