
React Refs All In One
https://reactjs.org/docs/react-api.html#refs
Ref
https://reactjs.org/docs/refs-and-the-dom.html
React.createRef
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.inputRef = React.createRef();
}
componentDidMount() {
this.inputRef.current.focus();
}
render() {
return <input type="text" ref={this.inputRef} />;
}
}
React.forwardRef
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>;
Forwarding refs to DOM components
Forwarding refs in higher-order-components
https://reactjs.org/docs/forwarding-refs.html
useRef
React Hooks
const refContainer = useRef(initialValue);
function TextInputWithFocusButton() {
const inputEl = useRef(null);
const onButtonClick = () => {
// `current` points to the mounted text input element
inputEl.current.focus();
};
return (
<>
<input ref={inputEl} type="text" />
<button onClick={onButtonClick}>Focus the input</button>
</>
);
}
https://reactjs.org/docs/hooks-reference.html#useref
React API
UMD + ESM
refs

xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!