如果我有一个第三方库,我通常会像这样初始化:
class App {
constructor(props) {
this.helperLibrary = new HelperLibrary();
this.state = { data: null };
}
}
Run Code Online (Sandbox Code Playgroud)
我将如何使用 React 钩子在功能组件中初始化它?
所以更新后的代码看起来像:
const App = () => {
const [data, setData] = useState(null);
// Would I write something like this here??
helperLibrary = new HelperLibrary();
// Or would I use something like useEffect since
// constructor runs when the component is created?
useEffect = (() => {
helperLibrary = new HelperLibrary();
}, []);
};
Run Code Online (Sandbox Code Playgroud)