Reactjs 在Redux中跨组件传递或共享数据
我是Redux的新手,所以这可能是我的商店设计或理解上的问题 我的应用程序中有两个组件:Reactjs 在Redux中跨组件传递或共享数据,reactjs,redux,Reactjs,Redux,我是Redux的新手,所以这可能是我的商店设计或理解上的问题 我的应用程序中有两个组件: 导航组件将根据显示的内容呈现链接列表 来自API调用的响应 内容组件获取内容 用于导航组件中选择的链接,并呈现HTML 这是我的hirearchy: - index.js (building store here) - actions - NavigationActions - ContentActions - components - Home - Navigation
- index.js (building store here)
- actions
- NavigationActions
- ContentActions
- components
- Home
- Navigation
- Content
- containers
- NavigationContainer
- ContentContainer
- reducers
- NavigationReduer (manages 'navigation' part of the store)
- ContentReducer (manages 'content' part of the store)
- RootReducer (combineReducers sits here)
我已经能够在应用程序加载时加载导航链接和内容(index.html)。当在导航组件中单击一个链接时,我会发送一个操作,将“selectedResource”键放在作为所选链接的存储的“导航”部分中
我的问题是如何与内容组件共享“selectedResource”,以便它加载新资源并呈现其HTML内容 在
ContentContainer
中,当您mapstatetops
时,您将执行以下操作:
const mapStateToProps = ({ NavigationState }) => {
return {
selectedResource: NavigationState.selectedResource
};
}
export default connect(mapStateToProps)(ContentContainer);
要检测ContentContainer
中的selectedResource
中的更改,您可以:
componentDidUpdate(prevProps) {
//May need some different comparison logic
if(this.props.selectedResource != prevProps.selectedResource) {
triggerNextAction();
}
}
谢谢什么是触发ContentContainer组件获取所选资源并重新呈现的下一个操作的最佳位置?编辑了我的答案