15
15
16
16
@implementation RCTSwitchComponentView {
17
17
UISwitch *_switchView;
18
- BOOL _wasOn;
19
18
}
20
19
21
20
- (instancetype )initWithFrame : (CGRect )frame
22
21
{
23
22
if (self = [super initWithFrame: frame]) {
24
- static const auto defaultProps = std::make_shared<const SwitchProps>();
25
- _props = defaultProps;
26
-
27
23
_switchView = [[UISwitch alloc ] initWithFrame: self .bounds];
28
24
29
25
[_switchView addTarget: self action: @selector (onChange: ) forControlEvents: UIControlEventValueChanged];
30
26
31
- _switchView.on = defaultProps->value ;
32
-
33
27
self.contentView = _switchView;
28
+
29
+ [self setPropsToDefault ];
34
30
}
35
31
36
32
return self;
37
33
}
38
34
35
+ - (void )setPropsToDefault
36
+ {
37
+ static const auto defaultProps = std::make_shared<const SwitchProps>();
38
+ _props = defaultProps;
39
+ _switchView.on = defaultProps->value ;
40
+ }
41
+
39
42
#pragma mark - RCTComponentViewProtocol
40
43
44
+ - (void )prepareForRecycle
45
+ {
46
+ [super prepareForRecycle ];
47
+ [self setPropsToDefault ];
48
+ }
49
+
41
50
+ (ComponentDescriptorProvider)componentDescriptorProvider
42
51
{
43
52
return concreteComponentDescriptorProvider<SwitchComponentDescriptor>();
@@ -51,7 +60,6 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
51
60
// `value`
52
61
if (oldSwitchProps.value != newSwitchProps.value ) {
53
62
_switchView.on = newSwitchProps.value ;
54
- _wasOn = newSwitchProps.value ;
55
63
}
56
64
57
65
// `disabled`
@@ -79,10 +87,10 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
79
87
80
88
- (void )onChange : (UISwitch *)sender
81
89
{
82
- if (_wasOn == sender.on ) {
90
+ const auto &props = *std::static_pointer_cast<const SwitchProps>(_props);
91
+ if (props.value == sender.on ) {
83
92
return ;
84
93
}
85
- _wasOn = sender.on ;
86
94
87
95
std::dynamic_pointer_cast<const SwitchEventEmitter>(_eventEmitter)
88
96
->onChange (SwitchOnChangeStruct{.value = static_cast <bool >(sender.on )});
0 commit comments