Giter Club home page Giter Club logo

Comments (6)

troutowicz avatar troutowicz commented on May 31, 2024 1

What about using refs?

...

onEachFeature() {
  // let map = this.refs.map.leafletElement
}

render() {
  return (
    <Map
      ref='map'
    >
    ...
    <GeoJson
      data={geoJSON}
      color='red'
      fillColor='green'
      onEachFeature={onEachFeature.bind(this)} />
    </Map>
  )
}

Calling bind is only necessary with ES6 classes.

from react-leaflet.

troutowicz avatar troutowicz commented on May 31, 2024

GeoJson is a descendant of MapLayer which adds the map prop to all children here. This means you have access to map via this.props.map inside the GeoJson component.

How are you trying to use the map prop?

from react-leaflet.

adanielyan avatar adanielyan commented on May 31, 2024

Ok, I think I wasn't clear enough, or maybe I am missing something.

I want to access the map from onEachFeature() function for my GeoJson layer. So I have this code:

<Map
    ...
    <GeoJson
        data={geoJSON}
        color='red'
        fillColor='green'
        map={this}
        onEachFeature={onEachFeature} />
</Map>

Now in onEachFeature(feature, layer) function I can access color and fillColor through this.color and this.fillColor respectively. But this.map is undefined even if I add it to <GeoJson> as map={this} along with color and fillColor.

What am I doing wrong?

from react-leaflet.

PaulLeCam avatar PaulLeCam commented on May 31, 2024

I think in your case the best solution is to implement a custom GeoJson component pretty much the same way as it is done in https://github.com/PaulLeCam/react-leaflet/blob/master/src/GeoJson.js
All you need is to add the onEachFeature handler to the props variable in componentWillMount, it would have access to the map directly.

from react-leaflet.

adanielyan avatar adanielyan commented on May 31, 2024

Thanks for responses. Using ref worked like a charm!

from react-leaflet.

troutowicz avatar troutowicz commented on May 31, 2024

Great :)

from react-leaflet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.