【发布时间】:2020-07-12 16:03:16
【问题描述】:
我正在尝试使用钩子将此类组件转换为功能组件
import React, { Component, cloneElement } from 'react';
class Dialog extends Component {
constructor(props) {
super(props);
this.id = uuid();
}
render(){
return ( <div>Hello Dialog</div> );
}
}
此组件使用特定 ID 启动,因为我可能必须使用它们的多个实例。如果我使用功能组件,如何实现这一点?
【问题讨论】:
-
您可以使用
React.useRef。如果您的 refcurrent为空,请尝试将其设置为uuid(),否则意味着您已经有了一个,您可以跳过它。
标签: javascript reactjs react-hooks