在React功能组件中,我们应该希望使用UseCallback,还是应该使功能变得纯净并保持组件之外?

发布于 2025-01-21 23:57:43 字数 102 浏览 2 评论 0原文

如果将功能保留在组件内部,则将在每个渲染上重新启动,这可以用Uusecallback预防,但这本身会有一定的成本。我们应该在组件,状态,状态更新机功能或作为参数所需的任何内容之外保留功能吗?

If function is kept inside the component it will get re-initialised on every render which can be prevented with useCallback but that itself has some cost. Should we keep functions outside the component and pass props, state, state updater functions or whatever is required as arguments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑叹一世浮沉 2025-01-28 23:57:43

是的,因为这样,管理函数将更方便,因此您只能构建一次并将动态值传递给它。
如果您在代码的每个组件中定义它,您将几次编写相同的功能,这根本无效

Yes, because this way it will be more convenient to manage the function, you build it only once and pass dynamic values ​​to it.
If you define it in every component of your code you will write the same function several times, which is not effective at all

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文