dnd Hook Api,并且想要调用父组件回调函数来更新useDrop drop函数中的状态,但是我只能从初始化状态更新父状态。
这里有一个例子,不管我怎么把div放到dropTarget中,计数总是更新为1,但是当点击按钮时,它可以像我预期的那样更新,为什么会发生这种情况?如何解决这个问题呢?
https://codesandbox.io/s/reactdndexample3-gpc61?fontsize=14
import React, { useState } from 'react';
import { __EXPERIMENTAL_DND_HOOKS_THAT_MAY_CHANGE_AND_BREAK_MY_BUILD__ as dnd, DropTarget } from 'react-dnd'
import { DragDropContext } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend'
const { useDrag,useDrop } = dnd
function HookTest(props) {
const [count,setCount] = useState(0);
return (
<div>
<button onClick={()=>{setCount(count + 1)}}>clickToAdd</button>
<DropDiv addCount={()=>{setCount(count + 1)}}></DropDiv>
<DragDiv></DragDiv>
<div>{count}</div>
</div>
);
}
export default DragDropContext(HTML5Backend)(HookTest)
function DropDiv(props) {
const [collectedProps, drop] = useDrop({
accept:"widget",
hover:(item,monitor) => {
},
drop:(item,monitor)=>props.addCount(),
collect:(monitor) => {
return {
isOver:monitor.isOver({shallow: true}),
}
}
})
return (
<div ref={drop} style={{width:"100px",height:"100px",background:"blue"}}>
dropTarget
</div>
)
}
function DragDiv(props) {
const [dragSource,drag] = useDrag({
item: {type:"widget"},
collect: monitor => ({
opacity: monitor.isDragging() ? 0.4 : 1,
}),
})
return (
<div ref={drag} style={{width:"100px",height:"100px",background:"red"}}>
DragSource
</div>
)
}发布于 2019-05-17 15:50:50
更新不起作用,因为在为onClick和addCount创建的函数的上下文中,count的值始终为0。
我建议您使用setCount的替代语法,并传递一个函数,该函数将获取以前的状态作为参数,并返回更新后的状态。
<button onClick={()=>{setCount(prevCount => prevCount + 1)}}>clickToAdd</button>
<DropDiv addCount={()=>{setCount(prevCount => prevCount + 1)}}></DropDiv>https://stackoverflow.com/questions/56181833
复制相似问题