use functional updates when updating state, fixes issue with square being deselected when hover/unhover event are triggered (#1112)

This commit is contained in:
Bryden Fogelman 2023-01-15 03:10:42 -08:00 committed by GitHub
parent b7d2254298
commit 4fabe482ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -35,25 +35,25 @@ export default function App() {
}, },
}) })
const onHoverShape: TLPointerEventHandler = (e) => { const onHoverShape: TLPointerEventHandler = (e) => {
setPageState({ setPageState((prevState) => ({
...pageState, ...prevState,
hoveredId: e.target, hoveredId: e.target,
}) }))
} }
const onUnhoverShape: TLPointerEventHandler = () => { const onUnhoverShape: TLPointerEventHandler = () => {
setPageState({ setPageState((prevState) => ({
...pageState, ...prevState,
hoveredId: null, hoveredId: null,
}) }))
} }
const onPointShape: TLPointerEventHandler = (info) => { const onPointShape: TLPointerEventHandler = (info) => {
setPageState({ ...pageState, selectedIds: [info.target] }) setPageState((prevState) => ({ ...prevState, selectedIds: [info.target] }))
} }
const onPointCanvas: TLPointerEventHandler = () => { const onPointCanvas: TLPointerEventHandler = () => {
setPageState({ ...pageState, selectedIds: [] }) setPageState((prevState) => ({ ...prevState, selectedIds: [] }))
} }
const onDragShape: TLPointerEventHandler = (e) => { const onDragShape: TLPointerEventHandler = (e) => {
@ -75,11 +75,11 @@ export default function App() {
const onPointerMove: TLPointerEventHandler = (info) => { const onPointerMove: TLPointerEventHandler = (info) => {
if (info.shiftKey) { if (info.shiftKey) {
setPageState((prev) => ({ setPageState((prevState) => ({
...pageState, ...prevState,
camera: { camera: {
...prev.camera, ...prevState.camera,
point: Vec.add(prev.camera.point, info.delta), point: Vec.add(prevState.camera.point, info.delta),
}, },
})) }))
} }