Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在Redux中跨组件传递或共享数据_Reactjs_Redux - Fatal编程技术网

Reactjs 在Redux中跨组件传递或共享数据

Reactjs 在Redux中跨组件传递或共享数据,reactjs,redux,Reactjs,Redux,我是Redux的新手,所以这可能是我的商店设计或理解上的问题 我的应用程序中有两个组件: 导航组件将根据显示的内容呈现链接列表 来自API调用的响应 内容组件获取内容 用于导航组件中选择的链接,并呈现HTML 这是我的hirearchy: - index.js (building store here) - actions - NavigationActions - ContentActions - components - Home - Navigation

我是Redux的新手,所以这可能是我的商店设计或理解上的问题

我的应用程序中有两个组件:

  • 导航组件将根据显示的内容呈现链接列表 来自API调用的响应
  • 内容组件获取内容 用于导航组件中选择的链接,并呈现HTML
  • 这是我的hirearchy:

    - 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组件获取所选资源并重新呈现的下一个操作的最佳位置?编辑了我的答案