提供顶级 App 组件状态是最佳做法吗?
Is it best practise to give the top level App component state?
我是 react/redux 的新手,想知道如何构建我的第一个应用程序。它将有一个 currentUser
以及应用程序级别状态的其他位。
connect()
我的 App
组件并向下传递此信息或直接连接需要访问此信息的组件(例如 NavBar)是否明智?
根据 http://redux.js.org/docs/faq/ReactRedux.html#react-multiple-components 上的 Redux FAQ 条目:
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.
我是 react/redux 的新手,想知道如何构建我的第一个应用程序。它将有一个 currentUser
以及应用程序级别状态的其他位。
connect()
我的 App
组件并向下传递此信息或直接连接需要访问此信息的组件(例如 NavBar)是否明智?
根据 http://redux.js.org/docs/faq/ReactRedux.html#react-multiple-components 上的 Redux FAQ 条目:
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.