possible slider android crash fix #12

merged
opened by whey.party targeting main

sorry react native reanimated is scary stuff

Changed files
+14 -8
src
components
forms
+14 -8
src/components/forms/Slider.tsx
···
-
import {useCallback, useEffect, useRef, useState} from 'react'
+
import {useCallback, useEffect, useRef} from 'react' // Removed useState
import {type StyleProp, View, type ViewStyle} from 'react-native'
import {Gesture, GestureDetector} from 'react-native-gesture-handler'
import Animated, {
···
const playHaptic = useHaptics()
const timerRef = useRef<NodeJS.Timeout | undefined>(undefined)
-
const [width, setWidth] = useState(0)
+
const width = useSharedValue(0)
const progress = useSharedValue(0)
const isPressed = useSharedValue(false)
···
.onBegin(e => {
isPressed.value = true
-
if (width > 0) {
-
const newProgress = Math.min(Math.max(e.x / width, 0), 1)
+
// FIX 2: Access width.value instead of width
+
if (width.value > 0) {
+
const newProgress = Math.min(Math.max(e.x / width.value, 0), 1)
progress.value = newProgress
handleValueChange(newProgress)
}
})
.onUpdate(e => {
-
if (width === 0) return
-
const newProgress = Math.min(Math.max(e.x / width, 0), 1)
+
// FIX 3: Access width.value instead of width
+
if (width.value === 0) return
+
const newProgress = Math.min(Math.max(e.x / width.value, 0), 1)
progress.value = newProgress
handleValueChange(newProgress)
})
···
})
const thumbAnimatedStyle = useAnimatedStyle(() => {
-
const translateX = progress.value * width
+
// FIX 4: Access width.value for calculations
+
const translateX = progress.value * width.value
return {
transform: [
{translateX: translateX - 12},
···
<View
style={[a.flex_1, a.justify_center, {cursor: 'pointer'}]}
// @ts-ignore web-only style
-
onLayout={e => setWidth(e.nativeEvent.layout.width)}>
+
// FIX 5: Update width.value directly on layout
+
onLayout={e => {
+
width.value = e.nativeEvent.layout.width
+
}}>
<View
style={[
a.w_full,