gpt4 book ai didi

javascript - React "lifting state up"哲学 : How to avoid ending up with one big parent component containing all the code?

转载 作者:行者123 更新时间:2023-11-30 20:01:31 25 4
gpt4 key购买 nike

这不是一个非常精确的问题。我想我理解“ react 提升状态”范式。据我所知,这是两个同级组件访问各自属性的唯一干净方式。

但这样做,我最终得到了一个包含所有内容的巨大类:所有子组件属性的数据信息,以及负责更新此信息的所有函数(负责调用 setState)。我对无法再将与它们相关的工作分派(dispatch)到子组件这一事实感到不满。

我的问题是:如何使用 React 避免父组件中所有代码的集中,同时保持良好的干净状态/prop 机制?还是我提示不对?

最佳答案

许多人认为直接跳转到 redux 是个好主意,但事实上,它会带来大量的计算开销和样板代码。

根据经验,您只需将逻辑向上移动树,这不仅与子组件相关,而且与兄弟组件相关。

您还应该看看 Presentational and Container Components 的概念.

关于javascript - React "lifting state up"哲学 : How to avoid ending up with one big parent component containing all the code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53323736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com