gpt4 book ai didi

javascript - 提供顶级应用程序组件状态是最佳实践吗?

转载 作者:太空宇宙 更新时间:2023-11-04 15:51:04 24 4
gpt4 key购买 nike

我是 React/Redux 新手,想知道如何构建我的第一个应用程序。它将具有 currentUser 以及应用程序级别状态的其他位。

connect() 我的 App 组件并传递此信息或直接连接需要访问此信息的组件(例如导航栏)是否明智?

最佳答案

根据 Redux 常见问题解答条目 http://redux.js.org/docs/faq/ReactRedux.html#react-multiple-components :

Early Redux documentation advised that you should only have a few connected components near the top of your component tree. However, time and experience has shown that that generally requires a few components to know too much about the data requirements of all their descendants, and forces them to pass down a confusing number of props.

The current suggested best practice is to categorize your components as “presentational” or “container” components, and extract a connected container component wherever it makes sense:

Emphasizing “one container component at the top” in Redux examples was a mistake. Don't take this as a maxim. Try to keep your presentation components separate. Create container components by connecting them when it's convenient. Whenever you feel like you're duplicating code in parent components to provide data for same kinds of children, time to extract a container. Generally as soon as you feel a parent knows too much about “personal” data or actions of its children, time to extract a container.

In general, try to find a balance between understandable data flow and areas of responsibility with your components.

关于javascript - 提供顶级应用程序组件状态是最佳实践吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43113811/

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