Fix excessive toggles on the Switch component (#26496)
Fix excessive toggles on the Switch component (#26496)

Summary:
In Windows, if you clicked on a Switch component to toggle it, you could see it "shimmy" back and forth once before settling. The native Switch ends up toggling three times every time it's invoked.

`Switch.js` prevents the native switch from toggling to the new value by explicitly setting the switch to `this.props.value` when it receives an `onChange` event. The re-setting of the value wasn't fast enough to prevent the `Switch` from starting to toggle, causing the visual shimmy.

The solution is taken from `TextInput`. `TextInput.js` stores `_lastNativeText` when it receives an `onChange` event. In `componentDidUpdate`, it puts `this.props.text` back on the native textbox if the value of `this.props.text` isn't the same as `_lastNativeText`, which is how it ensures that it is a controlled component. Porting this to the `Switch` results in only one toggle happening per invoke, removing the shimmy, while preserving the controlled component behavior.

This bug is not visible on Android or iOS, only Windows, however the code causing the bug was in `Switch.js` and it seems reasonable to avoid changing the value of the native switch excessively.

## Changelog

[General] [Fixed] - Fix excessive toggles on the Switch component
Pull Request resolved: https://github.com/facebook/react-native/pull/26496

Test Plan: Used RNTester on Android and iOS to test the Switch component and made sure that all scenarios behave as expected visually. Also ensured through the debugger that the value of the native switch is only being changed once, instead of three times.

Reviewed By: TheSavior

Differential Revision: D17468905

Pulled By: JoshuaGross

fbshipit-source-id: 92bf511510306968c3573ee4eed6df009850fd77

javache/react-native,hammerandchisel/react-native,exponent/react-native,exponentjs/react-native,facebook/react-native,janicduplessis/react-native,javache/react-native,exponentjs/react-native,pandiaraj44/react-native,facebook/react-native,javache/react-native,exponentjs/react-native,exponent/react-native,facebook/react-native,arthuralee/react-native,myntra/react-native,hammerandchisel/react-native,janicduplessis/react-native,hammerandchisel/react-native,arthuralee/react-native,arthuralee/react-native,facebook/react-native,exponent/react-native,janicduplessis/react-native,facebook/react-native,hoangpham95/react-native,janicduplessis/react-native,hoangpham95/react-native,pandiaraj44/react-native,pandiaraj44/react-native,myntra/react-native,javache/react-native,hammerandchisel/react-native,exponentjs/react-native,javache/react-native,hammerandchisel/react-native,javache/react-native,exponent/react-native,myntra/react-native,hoangpham95/react-native,facebook/react-native,hoangpham95/react-native,pandiaraj44/react-native,janicduplessis/react-native,janicduplessis/react-native,hoangpham95/react-native,myntra/react-native,pandiaraj44/react-native,pandiaraj44/react-native,hoangpham95/react-native,exponentjs/react-native,javache/react-native,pandiaraj44/react-native,javache/react-native,javache/react-native,exponentjs/react-native,myntra/react-native,hoangpham95/react-native,myntra/react-native,arthuralee/react-native,exponent/react-native,hammerandchisel/react-native,hoangpham95/react-native,exponent/react-native,janicduplessis/react-native,myntra/react-native,exponent/react-native,exponentjs/react-native,facebook/react-native,exponent/react-native,janicduplessis/react-native,myntra/react-native,pandiaraj44/react-native,facebook/react-native,hammerandchisel/react-native,facebook/react-native,hammerandchisel/react-native,exponentjs/react-native,arthuralee/react-native,myntra/react-native