I have a function component that takes a list from props and renders a simple select:
<AccountForm onSubmit={onAccountSave} countryList={countryList} account={account}></AccountForm>
In the compnent i render the select:
<select ref={register} className="form-control" name="countryId" disabled={!accountEditMode} >
{props.countryList.map((country, index) => (
<option key={country.id} value={country.country}>{country.country}</option>
))}
</select>
The countryList comes from an async call in the main component. I also gets the selected value from a prop. The default value is however not set, properly because I set the default value, before the select has been loaded. How do you handle a scenario like this? Is there an event or something I wait for before setting the value, or what is the right approach here?
<AccountForm />until you have a real data. For example{!isLoading && <AccountForm ...props /><AccountForm/>will be rendered only after the data fetched, so you don't have that problem. You'll pass the data and the selected value at once synchronically. BTW, I can't see in your code where you set the default value from the prop.