关于javascript:将子组件连接到存储与将父组件连接到存储并传递道具

Connecting child components to store vs connecting parent component to store and passing down props

经过大量搜索并使用React和React Native。我对哪一个仍然有相当模糊的意见
最好在什么情况下使用

  • 将父组件连接到存储,并将所有数据作为道具传递给子功能组件。我最初使用的是"反应"方式,但是很快我发现随着应用程序的增长,这个父组件处理的逻辑量开始变得又大又混乱。子组件也开始拥有自己的子组件依此类推。

  • 具有可正常使用的父组件(例如,Screen),并且需要从商店中获取信息的每个子组件都将与其连接。这是更"干净"的解决方案,但会创建很多不必要的存储连接"重复项"。

  • 使用Redux存储

    我的问题通常是更推荐使用的模式以及在哪些用例中,也很高兴知道拥有大量连接的(容器)组件的价格是多少


    由于每个问题都有其优点和缺点,因此不确定我是否可以对此问题提供正确或错误的答案。
    我的经验法则是仅当其父级是"道具的代理"时才连接深层嵌套的组件。也就是说,他们接受道具只是为了将道具传给他们的孩子。

    如果我可以从这个答案中引用(我自己):

    Avoid connecting components when you can and pass down the props to
    the children, the main reason for this is to prevent dependency on
    redux. I prefer keep my components"dumb" as i can and let them
    concern only on how things should look. I do have some components that
    concern on how things should work and these components are mainly
    dealing with logic and passing down data to the children, they are the
    components i often connect.

    When i notice that my app is scaling and some of my components are
    acting as a proxy of props (i even got a word for that!"Propxy"),
    that is they get props from their parent and pass them down without
    using them, i usually inject a connected component in the middle of
    the components tree so i can let the"propxy" components down the tree
    flow be more lighten and slim

    您还应该注意,连接的组件还有一个陷阱,那就是每个渲染都将触发mapstateToProps方法。如果那里有一些繁琐的逻辑,则应该记住它,通常使用reselect

    完成

    关于连接组件的好处,您可能已经意识到了。您可以通过react的上下文快速访问Provider的状态。

    编辑
    作为您评论的后续内容:

    about the rendering - wont I have much more unnecessary rendering if Ill have a deep nested children (common in medium to large apps) that will be unnecessarily re rendered on each parent update

    如果mapstateToProps的先前对象与返回的当前对象相同,则connect HOCpackage器将不会触发重新渲染。因此不会对您连接的组件进行不必要的重新渲染。
    您可以在本文中详细了解它的工作方式以及逻辑随时间的演变


    我使用第一个选项。
    您编写的缺点是正确的,但是我认为这样更容易调试和理解数据流。