react-dnd icon indicating copy to clipboard operation
react-dnd copied to clipboard

Is any call monitor.getItemType() return null at the beginDrag function call

Open lcoder opened this issue 9 years ago • 4 comments

what I'm code:

import ItemTypes from 'macro/ItemTypes' ;

const dragSource = {
    beginDrag( props , monitor ){
        let itemType = monitor.getItemType() ;
        console.log( itemType ) ;  // always return null whatever I drag any dragsource
        // cell 拖拽
        if( itemType == ItemTypes.EXIST_CARD ){
            let position = props.position ,
                arrayIndex = props.arrayIndex ;
            return {
                type: 'reRank' ,
                position ,
                arrayIndex
            }
        }else if( itemType == ItemTypes.ROWCARD ){
            return {} ;
        }else{
            console.warn( '未定义拖拽源' , itemType ) ;
            return {} ;
        }
    } ,
    canDrag( props , monitor ){
        let itemType = monitor.getItemType() ;
        let item = monitor.getItem() ;
        console.log( itemType )
        if( props.obj ){                // DragInner已经绑定了一个对象 ( 表单控件 描述对象 )
            return true ;
        }else{
            return false ;
        }
    }
}
export default dragSource ;

At beginDrag function console.log( itemType ) always return null .what evet I drag EXIST_CARD or ROWCARD dragSource .

DragSource:

@DragSource( ItemTypes.EXIST_CARD , dragSource , ( connect , monitor )=>{
    return {
        connectDragSource: connect.dragSource() 
    }
} )
export default class DragInner extends Component {
    static propTypes = {
        connectDropTarget: PropTypes.func.isRequired
    }
    constructor(props) {
      // ...
    }
    render() {
       // ...
        return connectDragSource(
            connectDropTarget(
              <div className={className} style={{
                    minHeight: '30px' ,
                    backgroundColor ,
                }} onClick={this.handleClick}>
                { /*hasDropped && <span>dropped {hasDroppedOnChild && ' on child'}</span>*/ }
                { modelToReact( obj ) }
              </div>
            )
        ) ;
    }
}

lcoder avatar Mar 16 '17 13:03 lcoder

@lcoder monitor are being used for internal state management of DND. When beginDrag (which should return information being passed to target, I am quoting documentation here), is called on DragSource we don't have ItemType information available in state. It would better to separate out the concerns. 'beginDrag' in the example is taking decision for different ItemTypes. If we separate them out, we can get rid of the check.

anilreddykatta avatar Mar 18 '17 07:03 anilreddykatta

@anilreddykatta I clear.Thanks!😄

lcoder avatar Mar 18 '17 10:03 lcoder

@lcoder Welcome.

anilreddykatta avatar Mar 18 '17 17:03 anilreddykatta

@lcoder Can you close this issue?

anilreddykatta avatar Mar 19 '17 04:03 anilreddykatta