-
Notifications
You must be signed in to change notification settings - Fork 734
Infra/ migrate SegmentedControl to reanimated v2 #1567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
testID | ||
} = props; | ||
const [selectedSegment, setSelectedSegment] = useState(-1); | ||
|
||
const animatedSelectedIndex = useSharedValue(selectedSegment); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that you have a shared value for the selectedIndex, you can implement this component a little differently.
Right now, you keep both animatedSelectedIndex
, selectedSegment
and indexRef
. Each one of them represent the same value which goes against the single source of truth rule..
I'd suggest to try and unify them all to a single source and use only the shareValue.
It can help with performance, since you won't need to update the state anymore and clean the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see why the indexRef
is unnecessary, but the selectedSegment
is necessary for triggering a render after the onLayout
(and after any onPress
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's go over it together..
Let me know when you have time.
Maybe I'm wrong, but I think it's possible to make some change and to make it work only with animatedSelectedIndex
const segmentsCounter = useRef(0); | ||
const animatedValue = useRef(new Reanimated.Value(initialIndex)); | ||
const delay = throttleTime || 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of having the delay
const you can set a default value to throttleTime
when you destruct it from props
testID | ||
} = props; | ||
const [selectedSegment, setSelectedSegment] = useState(-1); | ||
|
||
const animatedSelectedIndex = useSharedValue(selectedSegment); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's go over it together..
Let me know when you have time.
Maybe I'm wrong, but I think it's possible to make some change and to make it work only with animatedSelectedIndex
Description
disableThrottle
prop to allow disabling the trailing delay when changing the index. (resolves Why segmented control onChangeIndex so slow #1526)Changelog
Migrate SegmentedControl to reanimated v2 and add a
disableThrottle
prop