标签:extends net 问题 turn 代码 new ref code script
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>;
上述代码的解释:
1 function logProps(Component) {
2 class LogProps extends React.Component {
3 componentDidUpdate(prevProps) {
4 console.log(‘old props:‘, prevProps);
5 console.log(‘new props:‘, this.props);
6 }
7
8 render() {
9 const {forwardedRef, ...rest} = this.props;
11 // 把常规属性"forwardedRef"作为ref赋值给传过来的Component组件
12 return <Component ref={forwardedRef} {...rest} />;
13 }
14 }
15
16 // 注意React.forwardRef提供的第二个参数ref.
17 // 我们可以把ref作为一个常规属性"forwardedRef"传给LogProps这个组件
18 // 就可以跟实例绑定.
19 return React.forwardRef((props, ref) => {
20 return <LogProps {...props} forwardedRef={ref} />;
21 });
22 }
标签:extends net 问题 turn 代码 new ref code script
原文地址:https://www.cnblogs.com/dejunwang/p/11782484.html