鍍金池/ 問答/網(wǎng)絡安全  HTML/ react dnd插件中拖拽目標組件中spec的props.index一直是un

react dnd插件中拖拽目標組件中spec的props.index一直是undefined,這個要如何才能獲取到該值?

問題描述

react dnd插件中拖拽目標組件中spec的props.index一直是undefined,這個要如何才能獲取到該值?

問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法

使用React dnd 實現(xiàn)React項目的拖拽功能,在拖拽目標組件(Target)中,在hover(props, monitor, component) 組件中獲取specObj 對象方法中的props.index一直為undefined。
百度文檔中描述props: 組件當前的props
個人也采用了在拖拽目標組件(Target.js)模板中給子組件傳了index

相關代碼

...
const cardTarget = {
    hover(props, monitor) {
        console.log('props-->',props.index); //在控制臺打印出來是undefined
        //問題:我要如何才能獲取到這個值?
    }
};
class Target extends Component {
    render() { 
        ....
        let items = list.map((item, index) => {
            return <Source index={index} id={item.id} />
        })
        return connectDropTarget(<div>{items}</div>)
    }
}
...

你期待的結果是什么?實際看到的錯誤信息又是什么?

在hover組件中如何才能獲取到specObj 對象方法中的props.index的值?

控制臺打印信息:

props index--> undefined
回答
編輯回答
六扇門

你需要在beginDrag中去定義。

2018年5月2日 20:02