react-native-segmented-control / segmented-control

React Native SegmentedControl library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Does not behave as a controlled component on iOS

madox2 opened this issue · comments

I am trying to use segmented control as a controlled component through selectedIndex and onChange properties. On iOS it does not work as expected. In the simple example below I want to execute special logic when clicking 'More' item, but it is ignoring the value prop and keeps selecting 'More' item.

export function Navigation() {
  const [value, onChange] = useState(0);
  return (
    <SegmentedControl
      values={['One', 'Two', 'More']}
      selectedIndex={value}
      onChange={event => {
        const { selectedSegmentIndex } = event.nativeEvent;
        if (selectedSegmentIndex > 1) {
          // onChange(value); // does not work either
          return;
        }
        onChange(selectedSegmentIndex);
      }}
    />
  );
}

On Android it works as expected.
Version: "@react-native-segmented-control/segmented-control": "^2.4.2"