React.js 与外部组件的状态对象通信
React.js communicate with state object from outside component
使用 React DnD 库:
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
});
}
}
});
想知道如何通过 acceptDrop
函数将新的 "dropped" 项添加到我的 React 组件的 state
对象中? React 处理这类事情的常用方法是什么?
通常的方法是使用回调,在父组件中创建函数,然后在调用子组件时将其作为 props。
使用 React DnD 库:
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
});
}
}
});
想知道如何通过 acceptDrop
函数将新的 "dropped" 项添加到我的 React 组件的 state
对象中? React 处理这类事情的常用方法是什么?
通常的方法是使用回调,在父组件中创建函数,然后在调用子组件时将其作为 props。