Skip to content

Commit

Permalink
fix: slider text box not updating when value is updated from the parent
Browse files Browse the repository at this point in the history
  • Loading branch information
Vali-98 committed Dec 24, 2024
1 parent 27fdf15 commit cd1e47a
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/components/SliderInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Slider from '@react-native-community/slider'
import { Style } from 'constants/Global'
import { useState } from 'react'
import { useEffect, useState } from 'react'
import { View, Text, StyleSheet, TextInput } from 'react-native'

type SliderInputProps = {
Expand Down Expand Up @@ -31,6 +31,11 @@ const SliderInput: React.FC<SliderInputProps> = ({
}) => {
const [textValue, setTextValue] = useState(value.toString())

// This effect ensures that if `value` updates from the parent, this text is properly updated
useEffect(() => {
if (parseFloat(textValue) !== value) setTextValue(value.toString())
}, [value])

const handleSliderChange = (v: number) => {
if (!isNaN(clamp(v, min, max, precision))) onValueChange(v)
setTextValue(v.toString())
Expand Down

0 comments on commit cd1e47a

Please sign in to comment.