1
1
import _ from 'lodash' ;
2
- import React , { PureComponent } from 'react' ;
3
- import { asBaseComponent , forwardRef , BaseComponentInjectedProps , ForwardRefInjectedProps } from '../../commons/new' ;
2
+ import React , { PureComponent , GetDerivedStateFromProps } from 'react' ;
3
+ import {
4
+ asBaseComponent ,
5
+ forwardRef ,
6
+ BaseComponentInjectedProps ,
7
+ ForwardRefInjectedProps
8
+ } from '../../commons/new' ;
4
9
import View from '../view' ;
5
10
import RadioGroupContext from './RadioGroupContext' ;
6
11
@@ -19,7 +24,9 @@ interface RadioGroupState {
19
24
value ?: RadioGroupPropTypes [ 'initialValue' ] ;
20
25
}
21
26
22
- type Props = RadioGroupPropTypes & BaseComponentInjectedProps & ForwardRefInjectedProps ;
27
+ type Props = RadioGroupPropTypes &
28
+ BaseComponentInjectedProps &
29
+ ForwardRefInjectedProps ;
23
30
24
31
/**
25
32
* Wrap a group of Radio Buttons to automatically control their selection
@@ -35,10 +42,24 @@ class RadioGroup extends PureComponent<Props, RadioGroupState> {
35
42
} ;
36
43
}
37
44
38
- UNSAFE_componentWillReceiveProps ( nextProps : Props ) {
39
- if ( this . props . initialValue !== nextProps . initialValue ) {
40
- this . setState ( { value : nextProps . initialValue } ) ;
45
+ static getUpdatedState = (
46
+ nextProps : Props ,
47
+ prevState : RadioGroupState
48
+ ) : RadioGroupState | null => {
49
+ const { value} = prevState ;
50
+ const { initialValue} = nextProps ;
51
+
52
+ if ( _ . isUndefined ( nextProps . initialValue ) || value === initialValue ) {
53
+ return null ;
41
54
}
55
+
56
+ return {
57
+ value : initialValue
58
+ }
59
+ } ;
60
+
61
+ static getDerivedStateFromProps : GetDerivedStateFromProps < Props , RadioGroupState > = ( props , state ) => {
62
+ return RadioGroup . getUpdatedState ( props , state ) ;
42
63
}
43
64
44
65
getContextProviderValue ( ) {
@@ -62,4 +83,6 @@ class RadioGroup extends PureComponent<Props, RadioGroupState> {
62
83
}
63
84
}
64
85
65
- export default asBaseComponent < RadioGroupPropTypes > ( forwardRef ( RadioGroup ) ) ;
86
+ export { RadioGroup } ; // For tests
87
+
88
+ export default asBaseComponent ( forwardRef ( RadioGroup ) ) ;
0 commit comments