GithubHelp home page GithubHelp logo

Comments (8)

colbyfayock avatar colbyfayock commented on August 20, 2024

hey @biomiker I should probably build this into the component, but are you familiar with the forwardRef pattern?

https://reactjs.org/docs/forwarding-refs.html

If you set that up, you'll be able to create the ref in the IndexPage, and pass that ref just like you would on a native react component, and use that for the map. So something along the lines of:

const IndexPage = () => {
  const mapRef = createRef();
  ...
  <Map ref={mapRef}

then inside the Map component


const Map = React.forwardRef((props, ref) => (
  ...
  <BaseMap ref={ref}

does that help?

from gatsby-starter-leaflet.

colbyfayock avatar colbyfayock commented on August 20, 2024

Labeling for someone to pick up (if not me at some point). We should apply the update above to the starter itself

from gatsby-starter-leaflet.

biomiker avatar biomiker commented on August 20, 2024

OK after a few hiccups I did get this working, thanks! The wrinkles for me were:

  1. Inside the Map component I also had to eliminate mapRef and change mapRef to ref inside the useRefEffect hook.
  2. To access the actual leaflet map in the IndexPage, I need to use mapRef.current.leafletElement

Is #2 expected?

from gatsby-starter-leaflet.

colbyfayock avatar colbyfayock commented on August 20, 2024

yup that's expected, glad you were able to figure it out

that's how React Leaflet is storing it and making it available

feel free to make a PR if you're interested to add the ForwardRef functionality :)

from gatsby-starter-leaflet.

biomiker avatar biomiker commented on August 20, 2024

I think I could possibly manage that. :) Would it be a breaking change -- as the ref would become a different component?

from gatsby-starter-leaflet.

colbyfayock avatar colbyfayock commented on August 20, 2024

i dont think it would be breaking - since the way the Starter works is it spins up an entire new project with a new git history, it won't be like someone using a library trying to pull it in

additionally, we can set up the ref so that we have a "backup" in case someone doesn't explicitly pass one in

for instance:

const Map = ( props ) => {
  const { ...otherprops, ref ...restOfProps } = props;

  const backupRef = useRef();
  const mapRef = ref || backupRef;

if we do that, there will always be a ref available, but then it allows someone to pass in their own

from gatsby-starter-leaflet.

biomiker avatar biomiker commented on August 20, 2024

Good point. I'll try integrating this backupRef next week and check in a PR if it works. Thanks!!!

from gatsby-starter-leaflet.

colbyfayock avatar colbyfayock commented on August 20, 2024

this was added but it's moot at this point as react-leaflet v3 took away the abillity to use a ref on the Map component

from gatsby-starter-leaflet.

Related Issues (17)

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.