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

Search Results Not closing, and marker not dropping #101

Open
MuhammadJafarFA17BCS054 opened this issue Feb 22, 2021 · 9 comments
Open

Search Results Not closing, and marker not dropping #101

MuhammadJafarFA17BCS054 opened this issue Feb 22, 2021 · 9 comments

Comments

@MuhammadJafarFA17BCS054

Hi there,
I am having an issue with react-map-gl-geocoder. When i use "filter" prop to filter the search results, the geocoder's default marker doesnt drop on selected location, also the menu with search results doesnt collaps, it reopens.
I know you are going to ask me to memoize my handleViewPortChange function, the thing is I am new to react and i dont know how to do that. Can somebody please help me? Attaching the screenshot of my source code.

image
image

@MuhammadJafarFA17BCS054
Copy link
Author

@SamSamskies can you help me please?

@SamSamskies
Copy link
Owner

Hi @MuhammadJafarFA17BCS054, you are already memoizing the handleViewportChange function. However, you are not memoizing the function that you are passing to the filter prop. All objects and functions that are passed as props need to be memoized. https://reactjs.org/docs/hooks-reference.html#usecallback

@MuhammadJafarFA17BCS054
Copy link
Author

Hi @SamSamskies , Thankyou for helping me out, but as i said in the start, I am a newbie, i read the article but i still cant figure out how to memoize the function that i am passing to filter prop. Can you help me?

@MuhammadJafarFA17BCS054
Copy link
Author

#39 (comment)
I checked this code, I think it was written by you. It has the same error as mine. Can you make a new repo or something addressing the issue?

@SamSamskies
Copy link
Owner

SamSamskies commented Feb 24, 2021

You can use the useCallback hook to memoize functions. Without that a new function is created on every render which causes the geocoder to be re-initialized. Another solution would be to define the function outside of your component. That works because the reference to the function won't change every time the component renders.

Thanks for pointing out the issue with the broken sandbox. I fixed it.

@MuhammadJafarFA17BCS054
Copy link
Author

Thank You so much @SamSamskies , i got the basic idea what useCallBack do and what memoized functions are. What i am stuck on now is that, how do i call multiple functions (each made using a useCallBack hook) on onResult prop of Geocoder?

@SamSamskies
Copy link
Owner

You can wrap both of the functions with another function.

const sayHello = () => console.log("hello");
const sayWorld = () => console.log("world");
const sayHelloWorld = useCallback(() => {
  sayHello();
  sayWorld();
}, []);

@congdoe
Copy link

congdoe commented Mar 30, 2021

@MuhammadJafarFA17BCS054 - Were you able to resolve your issue. It seems I am having the same issue but for onResult prop.

@congdoe
Copy link

congdoe commented Mar 30, 2021

Nvm figured it out
image
image

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

3 participants