Listing 11-18: Using the useZipLookup custom hook
import {useRef,useState} from 'react';
import useZipLookup from './useZipLookup';
function ShippingAddress2(props){
const [zipcode,setZipcode] = useState('');
const [city,state] = useZipLookup(zipcode);
const setZip = (e)=>{
e.preventDefault();
setZipcode(e.target.zipcode.value);
}
return (
<form onSubmit={setZip}>
Zipcode: <input type="text" name="zipcode" />
<button type="submit">Lookup City/State</button><br />
City: {city}<br />
State: {state}<br />
</form>
)
}
export default ShippingAddress2;
Download the examples, report issues, and ask/answer questions in the discussion area by visiting the book's github page. All of the code for the book is also available on codesandbox.io for you to play around with.
ReactJS Foundations is published by John Wiley and Sons, Inc and is available in paperback and eBook.