Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State is reset when autocomplete is called #204

Open
santiagomed opened this issue Feb 11, 2023 · 2 comments
Open

State is reset when autocomplete is called #204

santiagomed opened this issue Feb 11, 2023 · 2 comments

Comments

@santiagomed
Copy link

santiagomed commented Feb 11, 2023

I am using the Autocomplete component and when onPlaceSelected I am saving the place into a state array (input) of the parent component. However, whenever a place is selected the previous value is set to an empty array and then adds the new selected place, instead of adding it to the existing array. I am thinking it has something to do with refs, but I'm not sure how to start fixing it. Any help or advice would be appreciated.

Example:

1. inputs = [place1];
2. onPlaceSelected={(place2) => {
          console.log(inputs); // prints empty array
          addInput(place2);
}}
4. input = [place2]; // should be input = [place1, place2]

// addInput basically does this
function addInput(place) {
          setInputs[...inputs, place]l;
}
@bsaff
Copy link

bsaff commented Apr 8, 2024

Experiencing something similar. The onPlaceSelected encapsulates a stale version of my parent state. I'd expect the function to get updated with the latest as props change, but it remains stale.

More on this mentioned here #168

@santiagomed
Copy link
Author

@bsaff are you using prev state when updating your state? This was my issue back then. So basically

setInputs(prev => [...prev, newInput])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants