简体   繁体   English

我们可以使用redux在全局范围内订阅事件侦听器,以避免将作为道具的功能从父级传递到多个子级吗

[英]Can we subsribe event listeners globally using redux to avoid passing a function as a prop from parent to multiple child

Example: 例:

App.jsx ( Parent component ) App.jsx(父组件)

import React, {Component} from 'react';
import Pane from './tabpanel/pane';
import Registry from '../utils/registry'
import SideNavBar from './sidenav/sidenav'; 

    /**
     * 
     * 
     * @class Application
     * @extends {Component}
     */
    class Application extends Component {
         constructor(props) {
            super(props);
            this.state = { sidenavFunc: '',  sidenavDirFunc: ''};
            this.callbackSideNavClick = this.callbackSideNavClick.bind(this);
            this.callbackSideNavDirClick = this.callbackSideNavDirClick.bind(this);

        }

        renderContent(content, navClickFunc, navDirClickFunc) {
            return content.map((item, index) => {
                if(item.type === 'component'){
                    let componentFn = Registry[item.value];

                    return componentFn ? componentFn(index, navClickFunc, navDirClickFunc) : item.value;
                }

                if(item.type === 'link'){
                    return (<iframe className="iframe-panel" src={item.value} />);
                }

                return "";
            });
        }

        callbackSideNavClick(inputFunc, e) {       
           this.setState({
               sidenavFunc: inputFunc.bind(e)
           });
        }

        callbackSideNavDirClick(inputFunc, e) {
            this.setState({
               sidenavDirFunc: inputFunc.bind(e)
           });
        }

        render() {
            const AppConfig = this.props.config;

            const items = AppConfig.children.map((item, index) => (

                        <Pane key={index} label={item.heading}>
                            <div className="content-class">{this.renderContent(item.content, this.callbackSideNavClick, this.callbackSideNavDirClick)}</div>
                        </Pane>
                    ));

            return (
                <div>
                    <div className="left-panel">
                        <label className="panel-title" >{AppConfig.heading}</label>
                        <SearchBar />
                         <SideNavBar callbackSideNavBarOptionClick={this.state.sidenavFunc} callbackDirFileClick={this.state.sidenavDirFunc} />
                    </div>
                </div>

            );
        }
    }

    export default Application;

As you can see in the above code I have passed 2 functions - callbackSideNavClick() and callbackSideNavDirClick() to another component Registry.js using the code below : 如您在上面的代码中看到的,我已经使用以下代码将2个函数-callbackSideNavClick()和callbackSideNavDirClick()传递给另一个组件Registry.js:

renderContent(content, navClickFunc, navDirClickFunc) {
            return content.map((item, index) => {
                if(item.type === 'component'){
                    let componentFn = Registry[item.value];

                    return componentFn ? componentFn(index, navClickFunc, navDirClickFunc) : item.value;
                }

                if(item.type === 'link'){
                    return (<iframe className="iframe-panel" src={item.value} />);
                }

                return "";
            });
        }

Registry.js Registry.js

/**
 * Add all React components to available as configuration
 * 
 * key: Put the name which will become component name in application configuration
 * value: function to returen component
 */

import React from 'react';
import FileViewerContainer from '../components/fileviewercontainer/fileviewercontainer';

const Registry = {
    FileViewerContainer: (index, navClickFunc, navDirClickFunc) =>  (<FileViewerContainer key={index} navClickFunc={navClickFunc} navDirClickFunc={navDirClickFunc} />)
};

export default Registry;

Like this I passed 2 functions to "FileViewerContainer" component and so on. 像这样,我将2个函数传递给“ FileViewerContainer”组件,依此类推。 My question is instead of passing these 2 function to multiple child as a prop, can I implement the same using any redux functionality. 我的问题是,不是将这2个函数作为道具传递给多个子对象,而是可以使用任何redux功能实现相同的功能。 Probably its possible. 可能是可能的。 can anyone please help me to achieve that.. 谁能帮我实现这一目标。

In redux, you can avoid passing functions through deep component hierarchies. 在redux中,您可以避免通过深层组件层次结构传递函数。 You can connect any component to the redux store using connect function from react-redux library and get your actions/functions using mapDispatchToProps (2nd argument): https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options 您可以使用react-redux库中的connect函数将任何组件连接到redux存储,并使用mapDispatchToProps (第二个参数)获取操作/功能: https : //github.com/reactjs/react-redux/blob/master/docs/ api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops选项

Example from the redux docs : https://github.com/reactjs/redux/blob/master/examples/real-world/src/containers/UserPage.js See the 2nd argument of export default connect on the bottom. 来自redux docs的示例https : //github.com/reactjs/redux/blob/master/examples/real-world/src/containers/UserPage.js参见底部的export default connect的第二个参数。 That's where you get your actions. 那就是您采取行动的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM