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 { |
// highlight-range{1-2}
|
||||
buttonRef = React.createRef(); |
const FancyButton = React.forwardRef((props, ref) => ( |
||||
|
<button ref={ref} className="FancyButton"> |
||||
focus() { |
{props.children} |
||||
this.buttonRef.current.focus(); |
</button> |
||||
} |
)); |
||||
|
|
||||
render() { |
// You can now get a ref directly to the DOM button:
|
||||
// highlight-next-line
|
const ref = React.createRef(); |
||||
const {label, theme, ...rest} = this.props; |
<FancyButton ref={ref}>Click me!</FancyButton>; |
||||
// highlight-range{5}
|
|
||||
return ( |
|
||||
<button |
|
||||
{...rest} |
|
||||
className={`${theme}-button`} |
|
||||
ref={this.buttonRef}> |
|
||||
{label} |
|
||||
</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} |
|
||||
/>; |
|
||||
|
@ -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) { |
// highlight-range{1-2}
|
||||
class Enhanced extends React.Component { |
const FancyButton = React.forwardRef((props, ref) => ( |
||||
// ...
|
<button ref={ref} className="FancyButton"> |
||||
|
{props.children} |
||||
render() { |
</button> |
||||
const {forwardedRef, ...rest} = this.props; |
)); |
||||
|
|
||||
// Assign the custom prop "forwardedRef" as a ref
|
// You can now get a ref directly to the DOM button:
|
||||
// highlight-next-line
|
const ref = React.createRef(); |
||||
return <Component ref={forwardedRef} {...rest} />; |
<FancyButton ref={ref}>Click me!</FancyButton>; |
||||
} |
|
||||
} |
|
||||
|
|
||||
// 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); |
|
||||
} |
|
||||
|
Loading…
Reference in new issue