@@ -43,6 +43,7 @@ const SegmentedControl = ({
43
43
const colorScheme = appearance || colorSchemeHook ;
44
44
const [ segmentWidth , setSegmentWidth ] = React . useState ( 0 ) ;
45
45
const animation = React . useRef ( new Animated . Value ( 0 ) ) . current ;
46
+ const ref = React . useRef ( ) ;
46
47
47
48
const handleChange = ( index : number ) => {
48
49
// mocks iOS's nativeEvent
@@ -56,6 +57,17 @@ const SegmentedControl = ({
56
57
onValueChange && onValueChange ( values [ index ] ) ;
57
58
} ;
58
59
60
+ const updateSegmentWidth = React . useCallback (
61
+ ( width : number ) => {
62
+ const newSegmentWidth = values . length ? width / values . length : 0 ;
63
+ if ( newSegmentWidth !== segmentWidth ) {
64
+ animation . setValue ( newSegmentWidth * ( selectedIndex || 0 ) ) ;
65
+ setSegmentWidth ( newSegmentWidth ) ;
66
+ }
67
+ } ,
68
+ [ values . length , segmentWidth , animation , selectedIndex ] ,
69
+ ) ;
70
+
59
71
React . useEffect ( ( ) => {
60
72
if ( animation && segmentWidth ) {
61
73
let isRTL = I18nManager . isRTL ? - segmentWidth : segmentWidth ;
@@ -68,8 +80,15 @@ const SegmentedControl = ({
68
80
}
69
81
} , [ animation , segmentWidth , selectedIndex ] ) ;
70
82
83
+ React . useEffect ( ( ) => {
84
+ if ( ref . current ) {
85
+ ref . current . measure ( ( _x , _y , width ) => updateSegmentWidth ( width ) ) ;
86
+ }
87
+ } , [ values ] ) ;
88
+
71
89
return (
72
90
< View
91
+ ref = { ref }
73
92
style = { [
74
93
styles . default ,
75
94
style ,
@@ -81,13 +100,7 @@ const SegmentedControl = ({
81
100
nativeEvent : {
82
101
layout : { width} ,
83
102
} ,
84
- } ) => {
85
- const newSegmentWidth = values . length ? width / values . length : 0 ;
86
- if ( newSegmentWidth !== segmentWidth ) {
87
- animation . setValue ( newSegmentWidth * ( selectedIndex || 0 ) ) ;
88
- setSegmentWidth ( newSegmentWidth ) ;
89
- }
90
- } } >
103
+ } ) => updateSegmentWidth ( width ) } >
91
104
{ ! backgroundColor && ! tintColor && (
92
105
< SegmentsSeparators
93
106
values = { values . length }
0 commit comments