Skip to content

Commit a973321

Browse files
authored
remove UNSAFE_componentWillReceiveProps, close bl00mber#124 (bl00mber#331)
1 parent 08f4ad4 commit a973321

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,12 @@ class PhoneInput extends React.Component {
240240
}
241241
}
242242

243-
UNSAFE_componentWillReceiveProps(nextProps) {
244-
if (nextProps.country !== this.props.country) {
245-
this.updateCountry(nextProps.country);
243+
componentDidUpdate(prevProps, prevState, snapshot) {
244+
if (prevProps.country !== this.props.country) {
245+
this.updateCountry(this.props.country);
246246
}
247-
else if (nextProps.value !== this.props.value) {
248-
this.updateFormattedNumber(nextProps.value);
247+
else if (prevProps.value !== this.props.value) {
248+
this.updateFormattedNumber(this.props.value);
249249
}
250250
}
251251

test/ReactPhoneInput.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,26 @@ describe('correct value update', () => {
188188
expect(phoneInput.querySelector('.selected-flag').children[0].className).toBe('flag 0')
189189
})
190190

191+
test('should rerender country without crashing', () => {
192+
const { container: phoneInput, rerender } = render(
193+
<PhoneInput
194+
country={undefined}
195+
/>)
196+
197+
rerender(
198+
<PhoneInput
199+
country="us"
200+
/>)
201+
202+
rerender(
203+
<PhoneInput
204+
country="es"
205+
/>)
206+
207+
expect(phoneInput.querySelector('.selected-flag').children.length).toBe(1)
208+
expect(phoneInput.querySelector('.selected-flag').children[0].className).toBe('flag es')
209+
})
210+
191211
it('renders one prefix when updated from empty value', () => {
192212
const { container: phoneInput, rerender } = render(
193213
<PhoneInput

0 commit comments

Comments
 (0)