Skip to content

Commit

Permalink
refactor: use ref instead of state
Browse files Browse the repository at this point in the history
  • Loading branch information
cazala committed Apr 27, 2023
1 parent eb06aad commit f341815
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions packages/@dcl/inspector/src/hooks/editor/useSnap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useState } from 'react'
import { useCallback, useEffect, useRef, useState } from 'react'
import { snapManager } from '../../lib/babylon/decentraland/snap-manager'
import { GizmoType } from '../../lib/utils/gizmo'

Expand Down Expand Up @@ -26,16 +26,15 @@ function setSnapValue(value: number, gizmo: GizmoType) {

export const useSnapState = (gizmo: GizmoType) => {
const [snap, setSnapInternal] = useState<string>(getSnapValue(gizmo).toString())
// TODO: useRef, avoid re-renders
const [skipSync, setSkipSync] = useState<boolean>(false)
const skipSyncRef = useRef(false)
const setSnap = useCallback((value: string, skipSync = false) => {
setSkipSync(skipSync)
skipSyncRef.current = skipSync
setSnapInternal(value)
}, [])

// send update to snap manager
useEffect(() => {
if (skipSync) return
if (skipSyncRef.current) return
const current = getSnapValue(gizmo)
const numeric = Number(snap)
if (snap === '' || isNaN(numeric) || numeric === current || numeric < 0) return
Expand All @@ -57,16 +56,16 @@ export const useSnapState = (gizmo: GizmoType) => {

export const useSnapToggle = () => {
const [isEnabled, setEnabledInternal] = useState<boolean>(snapManager.isEnabled())
const [skipSync, setSkipSync] = useState<boolean>(false)
const skipSyncRef = useRef(false)
const setEnabled = useCallback((value: boolean, skipSync = false) => {
setSkipSync(skipSync)
skipSyncRef.current = skipSync
setEnabledInternal(value)
}, [])
const toggle = useCallback(() => setEnabled(!isEnabled), [isEnabled])

// send update to snap manager
useEffect(() => {
if (skipSync) return
if (skipSyncRef.current) return
snapManager.setEnabled(isEnabled)
}, [isEnabled])

Expand Down

0 comments on commit f341815

Please sign in to comment.