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

How Use Draw Object with react Hooks ? #91

Open
pulnec opened this issue Mar 8, 2020 · 1 comment
Open

How Use Draw Object with react Hooks ? #91

pulnec opened this issue Mar 8, 2020 · 1 comment

Comments

@pulnec
Copy link

pulnec commented Mar 8, 2020

I am trying to enter the draw object with react hooks but an error message 'getAll is not function' is displayed

As the correct reference should be made, is its example with react component, as I can replicate it with react hooks, I have used the useRef () but it still does not work

`<Map
style="mapbox://styles/mapbox/streets-v9"
containerStyle={{
height: '100vh',
width: '100vw'
}}>
<DrawControl
ref={(drawControl) => { this.drawControl = drawControl; }}
/>

//...
handleButtonClick() {
this.drawControl.draw.getAll(); // Or any other API method
}`

in hooks
cons drawControl = useRef() ??

@nickdances
Copy link

nickdances commented Mar 26, 2020

Hi @pulnec , I know this issue is old but I just had the same problem and solved it by using useState instead of useRef. React will throw an error if you try to use useRef with the DrawControl.

const DrawableMap = (props) => {
  const [drawControlRef, setDrawControlRef] = useState(null);
  return (
      <Map>
          <DrawControl
          ref={(drawControl) => { setDrawControlRef(drawControl) }}
          />
      </Map>
  )
}

Note I've omitted the Map props in my example for brevity.

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