1

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?

4
  • Don't render <AccountForm /> until you have a real data. For example {!isLoading && <AccountForm ...props /> Commented May 20, 2020 at 12:03
  • @MoshFeu but wont you still have the same problem? I get my values async. Both the country list and the selected id. Then I start filling the select while setting the default value. Wont that override the default value as the select maybe has not been filled yet? Commented May 20, 2020 at 12:35
  • If I understand correctly, <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. Commented May 20, 2020 at 12:48
  • @MoshFeu I just tested it and it works perfectly. Thanks! Can you create an answer. Commented May 20, 2020 at 12:49

2 Answers 2

1

You can render <AccountForm /> only after the data been received so it won't have to deal with empty state.

Something like this (This is a "pseudo" implementation)

function Component() {
  const [countryList, setCountryList] = useState([]);
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    fetchCountryList()
      .then(data => {
        setCountryList(data);
        setIsLoading(false)
      });
  }, []);

  return (
    <>
    {!isLoading &&
      <AccountForm onSubmit={onAccountSave} countryList={countryList} account={account}></AccountForm>
    }
  )
}

Or you can check directly if countryList is empty, like this:

{!countryList.length &&
  <AccountForm onSubmit={onAccountSave} countryList={countryList} account={account}></AccountForm>
}

(And there are more options)

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried setting the value in the state, and then update the state in the async call? And setting that state value in option value? Just brainstorming.

3 Comments

Well, brainstorm is not an answer :) I know that you can't comment but you shouldn't commend in an answer..
So what you mean is have a hook. Set that as defaultValue. And update it. I will try.
@MoshFeu I know it's not by the rules, but I just wanted to help :) Yes, Thomas, that's the idea. Hope it helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.