React-mapbox-gl: How to get reference to map object?

Created on 3 Jun 2017  路  4Comments  路  Source: alex3165/react-mapbox-gl

This doesn't seem to work

ref={ map => { this.map = map } }

I want to do something like this, but I need reference for this.

Most helpful comment

For future googlers there is onStyleLoad which is called when map is available

<ReactMapboxGl onStyleLoad={ (map) => { ... } } />

All 4 comments

did you try access map object from state this.map.state.map?

refs should work after component is mounted

I tried this, but unfortunately it is null

  componentDidMount() {
    console.log(this.map.state.map)
  }

strange thing if I log this.map I see state.map WAT? Still looking into this

class ReactMapboxGlCustom extends ReactMapboxGl {
  componentDidMount() {
    ReactMapboxGl.prototype.componentDidMount.call(this)
    this.props.onMount && this.props.onMount(this.state.map) // this.state.map is null
  }
}

For future googlers there is onStyleLoad which is called when map is available

<ReactMapboxGl onStyleLoad={ (map) => { ... } } />
Was this page helpful?
0 / 5 - 0 ratings