1
- import React , { FC , useState , useEffect } from "react" ;
1
+ import React , { FC , useState , useEffect , useCallback , useMemo } from "react" ;
2
2
import {
3
3
SwitchSelectorWrapper ,
4
4
OptionItem ,
@@ -42,14 +42,17 @@ const SwitchSelector: FC<SwitchSelectorProps> = (props) => {
42
42
}
43
43
} , [ forcedSelectedIndex , options , selectedIndex ] ) ;
44
44
45
- const handleOnClick = ( index : number ) : void => {
46
- if ( ! disabled && index !== selectedIndex ) {
47
- setSelectedIndex ( index ) ;
48
- onChange ( options [ index ] . value ) ;
49
- }
50
- } ;
45
+ const handleOnClick = useCallback (
46
+ ( index : number ) : void => {
47
+ if ( ! disabled && index !== selectedIndex ) {
48
+ setSelectedIndex ( index ) ;
49
+ onChange ( options [ index ] . value ) ;
50
+ }
51
+ } ,
52
+ [ disabled , onChange , options , selectedIndex ]
53
+ ) ;
51
54
52
- const renderOptions = ( ) : React . ReactElement [ ] => {
55
+ const renderedOptions = useMemo ( ( ) => {
53
56
return options . map ( ( option , index ) => {
54
57
const isSelected = selectedIndex === index ;
55
58
const optionId = `${ name ?? "rss" } -option-${ index } ` ;
@@ -93,7 +96,17 @@ const SwitchSelector: FC<SwitchSelectorProps> = (props) => {
93
96
</ OptionItem >
94
97
) ;
95
98
} ) ;
96
- } ;
99
+ } , [
100
+ disabled ,
101
+ fontColor ,
102
+ fontSize ,
103
+ handleOnClick ,
104
+ name ,
105
+ optionBorderRadius ,
106
+ options ,
107
+ selectedFontColor ,
108
+ selectedIndex
109
+ ] ) ;
97
110
98
111
if ( ! options . length ) return null ;
99
112
return (
@@ -115,7 +128,7 @@ const SwitchSelector: FC<SwitchSelectorProps> = (props) => {
115
128
role = { "radiogroup" }
116
129
aria-labelledby = { name }
117
130
>
118
- { renderOptions ( ) }
131
+ { renderedOptions }
119
132
</ SwitchSelectorWrapper >
120
133
) ;
121
134
} ;
0 commit comments