From a261e6dfb2680a955943db53c4b0a7bb887bfe22 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Thu, 3 Oct 2019 03:58:00 -0700 Subject: [PATCH] Fix recycling of Switch Reviewed By: shergin Differential Revision: D17714863 fbshipit-source-id: 8a90a08328f007c782689b4e711d252bd2e22538 --- .../Switch/RCTSwitchComponentView.mm | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm b/React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm index c4183aecb17..478211bd4e6 100644 --- a/React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm @@ -15,29 +15,38 @@ using namespace facebook::react; @implementation RCTSwitchComponentView { UISwitch *_switchView; - BOOL _wasOn; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); - _props = defaultProps; - _switchView = [[UISwitch alloc] initWithFrame:self.bounds]; [_switchView addTarget:self action:@selector(onChange:) forControlEvents:UIControlEventValueChanged]; - _switchView.on = defaultProps->value; - self.contentView = _switchView; + + [self setPropsToDefault]; } return self; } +- (void)setPropsToDefault +{ + static const auto defaultProps = std::make_shared(); + _props = defaultProps; + _switchView.on = defaultProps->value; +} + #pragma mark - RCTComponentViewProtocol +- (void)prepareForRecycle +{ + [super prepareForRecycle]; + [self setPropsToDefault]; +} + + (ComponentDescriptorProvider)componentDescriptorProvider { return concreteComponentDescriptorProvider(); @@ -51,7 +60,6 @@ using namespace facebook::react; // `value` if (oldSwitchProps.value != newSwitchProps.value) { _switchView.on = newSwitchProps.value; - _wasOn = newSwitchProps.value; } // `disabled` @@ -79,10 +87,10 @@ using namespace facebook::react; - (void)onChange:(UISwitch *)sender { - if (_wasOn == sender.on) { + const auto &props = *std::static_pointer_cast(_props); + if (props.value == sender.on) { return; } - _wasOn = sender.on; std::dynamic_pointer_cast(_eventEmitter) ->onChange(SwitchOnChangeStruct{.value = static_cast(sender.on)});