简体   繁体   中英

React.js communicate with state object from outside component

Using the React DnD lib:

const itemDropTarget = {
    acceptDrop(component, item) {
        window.alert('You dropped ' + item.name + '!');
    }
};

const Container = React.createClass({

    mixins: [DragDropMixin],

    getInitialState() {
        return {
            items: []
        };
    },

    statics: {
        configureDragDrop(register) {
            register(ItemTypes.ITEM, {
                dropTarget: itemDropTarget
            });
        }
    }
});

Wondering how I can add the new "dropped" item into my React component's state object, from the acceptDrop function? What's the usual approach for this kind of thing with React?

通常的方法是使用回调,您可以在父组件内部创建函数,然后在调用子组件时将其作为道具。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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