Skip to content

Commit a261e6d

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
Fix recycling of Switch
Reviewed By: shergin Differential Revision: D17714863 fbshipit-source-id: 8a90a08328f007c782689b4e711d252bd2e22538
1 parent eb08b42 commit a261e6d

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm

+17-9
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,38 @@
1515

1616
@implementation RCTSwitchComponentView {
1717
UISwitch *_switchView;
18-
BOOL _wasOn;
1918
}
2019

2120
- (instancetype)initWithFrame:(CGRect)frame
2221
{
2322
if (self = [super initWithFrame:frame]) {
24-
static const auto defaultProps = std::make_shared<const SwitchProps>();
25-
_props = defaultProps;
26-
2723
_switchView = [[UISwitch alloc] initWithFrame:self.bounds];
2824

2925
[_switchView addTarget:self action:@selector(onChange:) forControlEvents:UIControlEventValueChanged];
3026

31-
_switchView.on = defaultProps->value;
32-
3327
self.contentView = _switchView;
28+
29+
[self setPropsToDefault];
3430
}
3531

3632
return self;
3733
}
3834

35+
- (void)setPropsToDefault
36+
{
37+
static const auto defaultProps = std::make_shared<const SwitchProps>();
38+
_props = defaultProps;
39+
_switchView.on = defaultProps->value;
40+
}
41+
3942
#pragma mark - RCTComponentViewProtocol
4043

44+
- (void)prepareForRecycle
45+
{
46+
[super prepareForRecycle];
47+
[self setPropsToDefault];
48+
}
49+
4150
+ (ComponentDescriptorProvider)componentDescriptorProvider
4251
{
4352
return concreteComponentDescriptorProvider<SwitchComponentDescriptor>();
@@ -51,7 +60,6 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
5160
// `value`
5261
if (oldSwitchProps.value != newSwitchProps.value) {
5362
_switchView.on = newSwitchProps.value;
54-
_wasOn = newSwitchProps.value;
5563
}
5664

5765
// `disabled`
@@ -79,10 +87,10 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
7987

8088
- (void)onChange:(UISwitch *)sender
8189
{
82-
if (_wasOn == sender.on) {
90+
const auto &props = *std::static_pointer_cast<const SwitchProps>(_props);
91+
if (props.value == sender.on) {
8392
return;
8493
}
85-
_wasOn = sender.on;
8694

8795
std::dynamic_pointer_cast<const SwitchEventEmitter>(_eventEmitter)
8896
->onChange(SwitchOnChangeStruct{.value = static_cast<bool>(sender.on)});

0 commit comments

Comments
 (0)