import React from 'react';
import { findDOMNode } from 'react-dom';
import { DropTarget } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
let newStyle = {'display':'none','left':'0px'} ;
let target = {
hover(props,monitor,component){
newStyle.display = 'block';
newStyle.left = monitor.getClientOffset().x-findDOMNode(component).getBoundingClientRect().left+'px';
//The current mouse position where the "on hover indicator" is expected
return;
},
drop(props, monitor,component) {
newStyle.display = 'none';
newStyle.left = '0px';
return props;
}
}
function collect(connect, monitor) {
return {
connectDropTarget: connect.dropTarget(),
};
}
class component extends React.Component {
constructor(props){
super(props);
}
render = () => {
const { connectDropTarget } = this.props;
return connectDropTarget(
<div>
<Span style = { newStyle }> On hover indicator </span>
// here another component drops wrapped within div!
</div>
)
}
}
export default DropTarget('type', target, collect)(component);
在悬停回调中,如果我记录 newStyle 对象的 left 属性,它会按预期显示当前鼠标位置,但不会更改渲染方法中 span 的样式。
任何帮助,将不胜感激。提前致谢。