React.js how to pass callbacks to child components?

后端 未结 2 1840
深忆病人
深忆病人 2020-12-14 15:42

I would like to pass a callback to a doubly nested component, and while I am able to pass the properties effectively, I can\'t figure out how to bind the callback to the cor

相关标签:
2条回答
  • 2020-12-14 16:23

    For your reference, please check the implementation I've created at jsfiddle.net/kb3gN/12007

    function ListenersService(){
        var listeners = {};
        this.addListener = function(callback){
            var id;
            if(typeof callback === 'function'){
                id = Math.random().toString(36).slice(2);
                listeners[id] = callback;
            }
            return id;
        }
        this.removeListener = function( id){
            if(listeners[id]){
                delete listeners[id];
                return true;
            }
            return false;
        }
        this.notifyListeners = function(data){
            for (var id in listeners) {
              if(listeners.hasOwnProperty(id)){
                listeners[id](data);
              }
            }
        }
    }
    
    function DataService(ListenersService){
        var Data = { value: 1 };
        var self = this;
    
        var listenersService = new ListenersService();
        this.addListener = listenersService.addListener;
        this.removeListener = listenersService.removeListener;
        this.getData = function(){
            return Data;
        }
    
        setInterval(function(){
            Data.value++;
            listenersService.notifyListeners(Data);
        }, 1000);
    }
    var dataSevice = new DataService(ListenersService);
    
    var World = React.createClass({
        render: function() {
            return <strong>{this.props.data.value}</strong>;
        }
    });
    
    var Hello = React.createClass({
        getInitialState: function() {
            return {
              data: this.props.dataService.getData()
            };
        },
        componentDidMount: function() {
            this.props.dataService.addListener(this.updateHandler)
        },
        updateHandler: function(data) {
            this.setState({
              data: data
            });
        },
        render: function() {
            return (
                <div>
                    Value: <World data={this.state.data} />
                </div>
            );
        }
    });
    
    React.renderComponent(<Hello dataService={dataSevice} />, document.body);
    
    0 讨论(0)
  • 2020-12-14 16:28

    You are passing this.onUserInput as a property to FirstNestedComponent. Therefore, you should access it in FirstNestedComponent as this.props.onUserInput.

    var FirstNestedComponent = React.createClass({
        render: function() {
            return (
                <div>
                    <SecondNestedComponent
                        onUserInput={this.props.onUserInput}
                    />
                </div>
            );
        }
    });
    
    0 讨论(0)
提交回复
热议问题