Address Autofill - React Quickstart
Address Autofill is a Mapbox Search JS feature that makes filling out postal addresses on web forms faster, easier, and more accurate. It presents the user with suggested address matches after they begin typing in your form. Users can click a suggested address and instantly fill in the remaining inputs (city, state, postal code, etc).
Address autofill allows users to enter their information with fewer keystrokes and provide more accurate information.
This Quickstart Guide will help you get started with Address Autofill using the Mapbox Search JS React framework.
To use Address Autofill in a website (without React), see the Address Autofill Web Quickstart.
Prerequisites
To use Mapbox Search JS, you need to have a Mapbox access token.
Your default public token
You can use your default public token to get started with the code snippets in this guide.
To get your default public token, login to account.mapbox.com and scroll down to the Access Tokens section.
This access token associates your search requests with a Mapbox account for billing. For more information on creating and using access tokens, see our token management documentation.
Installation
Install the NPM package.
npm install --save @mapbox/search-js-react
Once installed, the AddressAutofill
component and the useConfirmAddress
hook will be available for import into your React app.
Use the AddressAutofill
component
The <AddressAutofill>
component adds interactive search and autofill to your address form. It must be a descendant of a <form>
element must wrap <input>
elements with appropriate autocomplete attributes.
import { AddressAutofill } from '@mapbox/search-js-react';
const MyAddressForm = () => {
return (
<form>
<AddressAutofill
accessToken='YOUR_MAPBOX_ACCESS_TOKEN'
>
<input type="text" name="address-1" autocomplete="address-line1" />
<input type="text" name="address-2" autocomplete="address-line2" />
<input type="text" name="city" autocomplete="address-level2" />
<input type="text" name="state" autocomplete="address-level1" />
<input type="text" name="zip" autocomplete="postal-code" />
</AddressAutofill>
</form>
)
}
export default MyAddressForm
Try a full address autofill workflow in React, including address confirmation and minimap.
Address Confirmation
Address confirmation lets users match their address against our comprehensive database and presents a confirmation dialog. This workflow is usually triggered during form submission, providing a higher quality address to match the user's input if one can be found.
With the minimap
option, a small map image is presented with a pin for visual confirmation of an address. The user can move the pin to provide details about the physical delivery location that matches the address.
Address Confirmation for a form in a React app is implemented with the useConfirmAddress
hook. When called, it returns a ref
object which should be assigned to the address form, along with a function showConfirm()
which will show the confirmation UI.
showConfirm
returns an object with the type
property set to either 'change'
, 'nochange'
, or 'cancel'
based on the user's actions. You can then continue with form submission using the original address or the suggested address.
'change'
indicates that the autofill service found a better match and the user selected it for use. In this case, a feature
property is also included in the returned object which includes the suggested match. 'nochange'
indicates that the user chose to use the address they provided instead of the suggested address. 'cancel'
indicates that the user closed the confirmation dialog.
import React from 'react';
import {AddressAutofill, useConfirmAddress } from '@mapbox/search-js-react';
const MyAddressForm = () => {
const { formRef, showConfirm } = useConfirmAddress({
footer: 'My custom footer'
});
const handleSubmit = React.useCallback(async () => {
const result = await showConfirm();
// submit the original address if the user did not select a better match
if (result.type === 'nochange') submitForm();
// submit the address data from the better match chosen by the user
if (result.type === 'change') {
submitFormWithChanges(result.feature)
};
}, [showConfirm]);
return (
<div>
<form
ref={formRef}
onSubmit={handleSubmit}
>
<AddressAutofill
accessToken='YOUR_MAPBOX_ACCESS_TOKEN'
>
<input type="text" name="address-1" autocomplete="address-line1" />
<input type="text" name="address-2" autocomplete="address-line2" />
<input type="text" name="city" autocomplete="address-level2" />
<input type="text" name="state" autocomplete="address-level1" />
<input type="text" name="zip" autocomplete="postal-code" />
<AddressAutofill>
</form>
</div>
);
}
export default MyAddressForm
Try a full address autofill workflow in React, including address confirmation and minimap.