@@ -55,6 +55,7 @@ export interface RadioProps extends ViewProps {
5555export interface RadioState {
5656 sizeValue : Animated . Value ;
5757 checked ?: boolean ;
58+ control : 'state' | 'props' ;
5859}
5960
6061export default class Radio extends Component < RadioProps , RadioState > {
@@ -69,39 +70,41 @@ export default class Radio extends Component<RadioProps, RadioState> {
6970 this . state = {
7071 checked : props . checked ,
7172 sizeValue : new Animated . Value ( 0 ) ,
73+ control : 'state' ,
7274 } ;
7375 }
7476 componentDidMount ( ) {
75- // this.setState({
76- // checked: this.props.checked,
77- // });
7877 this . animatedStart ( this . props . checked ) ;
7978 }
80- UNSAFE_componentWillReceiveProps ( nextProps : RadioProps ) {
81- if ( nextProps . checked !== this . props . checked ) {
82- this . setState ( { checked : nextProps . checked } , ( ) => {
83- this . animatedStart ( nextProps . checked ) ;
84- } ) ;
79+ static getDerivedStateFromProps ( props : RadioProps , state : RadioState ) {
80+ if ( state . control === 'state' && props . checked === state . checked ) {
81+ return {
82+ control : 'props' ,
83+ } ;
8584 }
86- }
87- animatedStart ( checked ?: boolean ) {
88- if ( checked ) {
89- Animated . spring ( this . state . sizeValue , {
90- toValue : this . props . thumbSize ! ,
91- overshootClamping : true ,
92- useNativeDriver : false ,
93- } ) . start ( ) ;
94- } else {
95- Animated . spring ( this . state . sizeValue , {
96- toValue : 0 ,
85+ if ( props . checked !== state . checked ) {
86+ Animated . spring ( state . sizeValue , {
87+ toValue : ! ! props . checked ? props . thumbSize ! : 0 ,
9788 overshootClamping : true ,
9889 useNativeDriver : false ,
9990 } ) . start ( ) ;
91+ return {
92+ checked : props . checked ,
93+ control : 'props' ,
94+ } ;
10095 }
96+ return null ;
97+ }
98+ animatedStart ( checked ?: boolean ) {
99+ Animated . spring ( this . state . sizeValue , {
100+ toValue : ! ! checked ? this . props . thumbSize ! : 0 ,
101+ overshootClamping : true ,
102+ useNativeDriver : false ,
103+ } ) . start ( ) ;
101104 }
102105 handlePress = ( event : GestureResponderEvent ) => {
103106 const { onPress } = this . props ;
104- this . setState ( { checked : true } , ( ) => {
107+ this . setState ( { checked : true , control : 'state' } , ( ) => {
105108 this . animatedStart ( true ) ;
106109 onPress && onPress ( event ) ;
107110 } ) ;
0 commit comments