Skip to content

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

Merged
merged 7 commits into from
Oct 14, 2021

Conversation

lidord-wix
Copy link
Contributor

Description

Changelog

Migrate SegmentedControl to reanimated v2 and add a disableThrottle prop

testID
} = props;
const [selectedSegment, setSelectedSegment] = useState(-1);

const animatedSelectedIndex = useSharedValue(selectedSegment);
Copy link
Collaborator

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.

Copy link
Contributor Author

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).

Copy link
Collaborator

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

@lidord-wix lidord-wix requested a review from ethanshar October 10, 2021 08:53
const segmentsCounter = useRef(0);
const animatedValue = useRef(new Reanimated.Value(initialIndex));
const delay = throttleTime || 0;
Copy link
Collaborator

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);
Copy link
Collaborator

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

@lidord-wix lidord-wix requested a review from ethanshar October 13, 2021 13:11
@ethanshar ethanshar merged commit 23394c5 into master Oct 14, 2021
@lidord-wix lidord-wix deleted the Infra/segmentedControl_reanimated2 branch November 1, 2021 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Why segmented control onChangeIndex so slow
2 participants