我仍然在思考react-hooks,但努力想知道我在这里做错了什么。我有一个用于调整面板大小的组件,onmousedown
在边缘我更新状态值,然后有一个mousemove
使用该值的事件处理程序,但是在值更改后它似乎没有更新。
这是我的代码:
export default memo(() => {
const [activePoint, setActivePoint] = useState(null); // initial is null
const handleResize = () => {
console.log(activePoint); // is null but should be 'top|bottom|left|right'
};
const resizerMouseDown = (e, point) => {
setActivePoint(point); // setting state as 'top|bottom|left|right'
window.addEventListener('mousemove', handleResize);
window.addEventListener('mouseup', cleanup); // removed for clarity
};
return (
<div className="interfaceResizeHandler">
{resizePoints.map(point => (
<div
key={ point }
className={ `interfaceResizeHandler__resizer interfaceResizeHandler__resizer--${ point }` }
onMouseDown={ e => resizerMouseDown(e, point) }
/>
))}
</div>
);
});
问题在于handleResize
函数,这应该使用最新版本的activePoint
字符串,top|left|bottom|right
而不是null
.