diff --git a/Libraries/Components/Switch/Switch.js b/Libraries/Components/Switch/Switch.js index 84a05067541..483d2dc2461 100644 --- a/Libraries/Components/Switch/Switch.js +++ b/Libraries/Components/Switch/Switch.js @@ -18,11 +18,13 @@ const StyleSheet = require('../../StyleSheet/StyleSheet'); import AndroidSwitchNativeComponent, { Commands as AndroidSwitchCommands, } from './AndroidSwitchNativeComponent'; +import SwitchNativeComponent, { + Commands as SwitchCommands, +} from './SwitchNativeComponent'; import type {ColorValue} from '../../StyleSheet/StyleSheetTypes'; import type {SyntheticEvent} from '../../Types/CoreEventTypes'; import type {ViewProps} from '../View/ViewPropTypes'; -import SwitchNativeComponent from './SwitchNativeComponent'; type SwitchChangeEvent = SyntheticEvent< $ReadOnly<{| @@ -208,7 +210,7 @@ class Switch extends React.Component { const nativeProps = {}; const value = this.props.value === true; - if (this._lastNativeValue !== value && typeof value === 'boolean') { + if (this._lastNativeValue !== value) { nativeProps.value = value; } @@ -223,7 +225,7 @@ class Switch extends React.Component { nativeProps.value, ); } else { - this._nativeSwitchRef.setNativeProps(nativeProps); + SwitchCommands.setValue(this._nativeSwitchRef, nativeProps.value); } } } diff --git a/Libraries/Components/Switch/SwitchNativeComponent.js b/Libraries/Components/Switch/SwitchNativeComponent.js index 13ac5b46d2c..262f3d0dea9 100644 --- a/Libraries/Components/Switch/SwitchNativeComponent.js +++ b/Libraries/Components/Switch/SwitchNativeComponent.js @@ -13,8 +13,10 @@ import type {BubblingEventHandler, WithDefault} from '../../Types/CodegenTypes'; import type {ColorValue} from '../../StyleSheet/StyleSheetTypes'; import type {ViewProps} from '../View/ViewPropTypes'; +import * as React from 'react'; import codegenNativeComponent from '../../Utilities/codegenNativeComponent'; +import codegenNativeCommands from 'react-native/Libraries/Utilities/codegenNativeCommands'; import type {HostComponent} from '../../Renderer/shims/ReactNativeTypes'; type SwitchChangeEvent = $ReadOnly<{| @@ -40,6 +42,17 @@ type NativeProps = $ReadOnly<{| onChange?: ?BubblingEventHandler, |}>; +type ComponentType = HostComponent; + +interface NativeCommands { + +setValue: (viewRef: React.ElementRef, value: boolean) => void; +} + +export const Commands: NativeCommands = codegenNativeCommands({ + supportedCommands: ['setValue'], +}); + export default (codegenNativeComponent('Switch', { paperComponentName: 'RCTSwitch', -}): HostComponent); + excludedPlatform: 'android', +}): ComponentType);