ErrorPro / react-google-autocomplete

React components for google places API.
MIT License
473 stars 114 forks source link

usePlacesWidget remove my state #145

Closed teddyboirin closed 2 years ago

teddyboirin commented 2 years ago

Hello! i don't know why but my previous state (others input) are removed when I use usePlacesWidget :

const { ref } = usePlacesWidget({ apiKey: 'api', onPlaceSelected: (places) => { setForm({ ...form, city: places.address_components[2].long_name, postcode: places.address_components[6].long_name, }); }, language: 'fr', options: { types: ['address'], componentRestrictions: { country: 'fr' }, }, });

ErrorPro commented 2 years ago

@teddyboirin Hey, I believe it's the ({ ...form, ... }) that basically is empty. You can just change a single state inside the onPlaceSelected callback hence making it more isolating. onPlaceSelected: (places) => { setFieldValue('myPlace', places: places.address_components[2].long_name) }