From 4a9c4198b0801c3bb427892a063a3f8590f48f08 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 10 Aug 2023 19:30:43 +0100 Subject: [PATCH] Avoid using state for immutable fields (#11397) --- src/components/views/auth/CountryDropdown.tsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/components/views/auth/CountryDropdown.tsx b/src/components/views/auth/CountryDropdown.tsx index dc8ff2e1a3..a1b428de68 100644 --- a/src/components/views/auth/CountryDropdown.tsx +++ b/src/components/views/auth/CountryDropdown.tsx @@ -50,10 +50,11 @@ interface IProps { interface IState { searchQuery: string; - defaultCountry: PhoneNumberCountryDefinition; } export default class CountryDropdown extends React.Component { + private readonly defaultCountry: PhoneNumberCountryDefinition; + public constructor(props: IProps) { super(props); @@ -78,9 +79,9 @@ export default class CountryDropdown extends React.Component { } } + this.defaultCountry = defaultCountry ?? COUNTRIES[0]; this.state = { searchQuery: "", - defaultCountry: defaultCountry ?? COUNTRIES[0], }; } @@ -89,7 +90,7 @@ export default class CountryDropdown extends React.Component { // If no value is given, we start with the default // country selected, but our parent component // doesn't know this, therefore we do this. - this.props.onOptionChange(this.state.defaultCountry); + this.props.onOptionChange(this.defaultCountry); } } @@ -150,7 +151,7 @@ export default class CountryDropdown extends React.Component { // default value here too, otherwise we need to handle null / undefined // values between mounting and the initial value propagating - const value = this.props.value || this.state.defaultCountry.iso2; + const value = this.props.value || this.defaultCountry.iso2; return (