If you haven’t already, read part 1 before starting.
Mapbox
We are now going to see how we could search for cities calling the mapbox API. In order to use the API, you’ll need to create an account (it’s free, even if they might ask for a credit card in order to confirm your identity).
Now that you have your account created, get the Default public token from the account page.
In order to call the API, we are going to use axios.
|
|
Saving the user input
Now that we have all the libs we need, we are going to start “saving” what the user is typing in the component state.
|
|
Calling the API
We are going to create a function called callApi
this will only call the mapbox endpoint with the value we just saved in the state.
Here’s the documentation for the endpoint we are going to use.
We simply need to call the GET https://api.mapbox.com/geocoding/v5/{endpoint}/{search_text}.json?access_token={access_token}&types={types}
endpoint where:
endpoint
will bemapbox.places
in our casesearch_text
will be the value we saved in our local stateaccess_token
the token you got from your account pagetypes
will beplace
because we want to see the cities (see here for more info)
|
|
Once we do get the results, we want to save the cities into a new variable in the component state.
Also we do not need to save everything, so for now we are going to save a name and the GPS location.
|
|
And obviously if we want to display the values from Mapbox rather than the hardcoded list, we need to delete the cities
const we created in Part 1.
What we want now is, every time we get a new key stroke, we want to call mapbox and request the list of cities.
For that let’s use useEffect
and call our callApi
function every time the searchText
changes.
|
|
So this is working as expected, every time we type a letter, it will call the mapbox api and returns the list of cities.
Optimisation
This is not very optimised, most of the calls won’t really be needed. When you type a city’s name, you might want to type x letters before it actually does the search, and by doing that not use the entire quote of requests.
In order to fix that, we are going to create a debounce hook, that will call our function only after x milliseconds.
We are going to create a basic one, if you want to cover all scenarios, you might want to use a fancy one from a third party library.
|
|
If you’ve never used useRef
:
useRef returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for the full lifetime of the component.
In our case we don’t want the timer var to be recreated every re render, otherwise it will defeat the purpose of this timer.
So for every key stroke, the method will be called, and the timer will be reset until the timer ends. Once the delay is reached, the callback will be called.
Lets see how we use it in our main component:
|
|
So now when you type, the endpoint will only be called if you stop typing for 500ms.
Next step Part 3