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

Get the initialization event to use draw.(...) #187

Open
CMonjo opened this issue Sep 30, 2020 · 1 comment
Open

Get the initialization event to use draw.(...) #187

CMonjo opened this issue Sep 30, 2020 · 1 comment

Comments

@CMonjo
Copy link

CMonjo commented Sep 30, 2020

Hello,

First of all a big congratulations for the package it is really great and very useful for my use! :)

I cannot solve a problem, despite the fact that I have read all the documentation several times.
I initiate my DrawControl component this way with a ref to be able to use draw.()

<DrawControl ref={(drawControl) => { this.drawControl = drawControl; }}/>
When my parent component is assembled, in componentDidMount I would like to be able to follow an API call to draw all the features that I have recovered. Like this :

this.drawControl.draw.add(feature);

I get this error (normal):

Unhandled Rejection (TypeError): Cannot read property 'draw' of undefined

How do I know when is the DrawControl component init and the draw function usable?

Thank you very much! Hoping to find an answer :(

@TeodorSK
Copy link

I had a similar issue, except I wanted my features to draw whenever I add a feature to the component state. Updating the component state forced the whole component, including the Map and DrawControl, to re-render and I would get the same error.

A temporary workaround is to do setTimeout(this.drawControl.draw.add(feature), 1000), which simply waits 1000ms before it calls add(). This lets the DrawControl ref initialize before add() gets called, but I'm certain there's a better solution.

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