I want to assign a value to one of the initial values (referrerId), based on whether URL parameters exist or not. I have left out the other code which I believe is irrelevant for this question. When the form loads, I want the referrerId field to be prepopulated with id extracted from URL parameters.
<Form
initialValues={{
referrerId: // if there are no parameters set string "none" : referrer's id, which is extracted from params,
}}
>
<FormField
label="Referrer's Id"
name="referrerId"
placeholder="Referrer's Id"
type="text"
/>
</Form>
One way to make this possible is by using the URLSearchParams javascript interface.
const parameters = new URLSearchParams(window.location.search);
<Form
initialValues={{
referrerId: `${parameters.get('referrer-id')}`,
}}
>
<FormField
label="Referrer's Id"
name="referrerId"
placeholder="Referrer's Id"
type="text"
/>
</Form>
and your url will look something like
https://mycoolwebsite.com/somepage?referrer-id=23221