小编dan*_*yyy的帖子

使用 React 钩子时如何初始化第三方库?

如果我有一个第三方库,我通常会像这样初始化:

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)

reactjs react-hooks

6
推荐指数
1
解决办法
2513
查看次数

标签 统计

react-hooks ×1

reactjs ×1