Browse Source
* Add 'visual components' use case for forwarding refs * Rearrange "forwarding refs" to focus on simple use case * Minor wording nits to 2018-03-29-react-v-16-3.md * Minor wording nits to forwarding-refs.md * Add more info to the forwardRef reference doc * Minor wording nits to reference-react.mdmain
Lucas Duailibe
7 years ago
committed by
Dan Abramov
7 changed files with 88 additions and 69 deletions
@ -1,32 +1,10 @@ |
|||
class FancyButton extends React.Component { |
|||
buttonRef = React.createRef(); |
|||
|
|||
focus() { |
|||
this.buttonRef.current.focus(); |
|||
} |
|||
|
|||
render() { |
|||
// highlight-next-line
|
|||
const {label, theme, ...rest} = this.props; |
|||
// highlight-range{5}
|
|||
return ( |
|||
<button |
|||
{...rest} |
|||
className={`${theme}-button`} |
|||
ref={this.buttonRef}> |
|||
{label} |
|||
// highlight-range{1-2}
|
|||
const FancyButton = React.forwardRef((props, ref) => ( |
|||
<button ref={ref} className="FancyButton"> |
|||
{props.children} |
|||
</button> |
|||
); |
|||
} |
|||
} |
|||
|
|||
// highlight-next-line
|
|||
const FancyThemedButton = withTheme(FancyButton); |
|||
)); |
|||
|
|||
// We can render FancyThemedButton as if it were a FancyButton
|
|||
// It will automatically receive the current "theme",
|
|||
// And the HOC will pass through our other props.
|
|||
<FancyThemedButton |
|||
label="Click me!" |
|||
onClick={handleClick} |
|||
/>; |
|||
// You can now get a ref directly to the DOM button:
|
|||
const ref = React.createRef(); |
|||
<FancyButton ref={ref}>Click me!</FancyButton>; |
|||
|
@ -0,0 +1,10 @@ |
|||
// highlight-range{1-2}
|
|||
const FancyButton = React.forwardRef((props, ref) => ( |
|||
<button ref={ref} className="FancyButton"> |
|||
{props.children} |
|||
</button> |
|||
)); |
|||
|
|||
// You can now get a ref directly to the DOM button:
|
|||
const ref = React.createRef(); |
|||
<FancyButton ref={ref}>Click me!</FancyButton>; |
@ -0,0 +1,7 @@ |
|||
function FancyButton(props) { |
|||
return ( |
|||
<button className="FancyButton"> |
|||
{props.children} |
|||
</button> |
|||
); |
|||
} |
@ -1,28 +1,10 @@ |
|||
function enhance(Component) { |
|||
class Enhanced extends React.Component { |
|||
// ...
|
|||
|
|||
render() { |
|||
const {forwardedRef, ...rest} = this.props; |
|||
|
|||
// Assign the custom prop "forwardedRef" as a ref
|
|||
// highlight-next-line
|
|||
return <Component ref={forwardedRef} {...rest} />; |
|||
} |
|||
} |
|||
|
|||
// Intercept the "ref" and pass it as a custom prop, e.g. "forwardedRef"
|
|||
// highlight-range{1-3}
|
|||
function enhanceForwardRef(props, ref) { |
|||
return <Enhanced {...props} forwardedRef={ref} />; |
|||
} |
|||
|
|||
// These next lines are not necessary,
|
|||
// But they do give the component a better display name in DevTools,
|
|||
// e.g. "ForwardRef(withTheme(MyComponent))"
|
|||
const name = Component.displayName || Component.name; |
|||
enhanceForwardRef.displayName = `enhance(${name})`; |
|||
|
|||
// highlight-next-line
|
|||
return React.forwardRef(enhanceForwardRef); |
|||
} |
|||
// highlight-range{1-2}
|
|||
const FancyButton = React.forwardRef((props, ref) => ( |
|||
<button ref={ref} className="FancyButton"> |
|||
{props.children} |
|||
</button> |
|||
)); |
|||
|
|||
// You can now get a ref directly to the DOM button:
|
|||
const ref = React.createRef(); |
|||
<FancyButton ref={ref}>Click me!</FancyButton>; |
|||
|
Loading…
Reference in new issue