Comments (8)
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.
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.
OK after a few hiccups I did get this working, thanks! The wrinkles for me were:
- Inside the Map component I also had to eliminate mapRef and change mapRef to ref inside the useRefEffect hook.
- To access the actual leaflet map in the IndexPage, I need to use mapRef.current.leafletElement
Is #2 expected?
from gatsby-starter-leaflet.
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.
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.
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.
Good point. I'll try integrating this backupRef next week and check in a PR if it works. Thanks!!!
from gatsby-starter-leaflet.
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)
- Tooltip not centered on gatsby develop
- yarn format fails, cannot commit to my clone of this project HOT 1
- Could not resolve eslint dependency HOT 7
- Parsing error: The keyword 'import' is reserved HOT 7
- what would it take to port this to gatsby v3? HOT 4
- test HOT 4
- Live watch mode HOT 3
- Update Readme.md
- Leaflet starter appears broken HOT 3
- 'Center' of map is incorrect (in development mode only?) HOT 5
- Update package for "react": "^18" HOT 1
- Add additional default map services and projection options
- Create tests using leaflet mock HOT 7
- Add support for different projections
- dependency conflict HOT 5
- Find visitor's location and zoom demo header to that location HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gatsby-starter-leaflet.