Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
How to pass state down to child component's handler?

The GeoJson component has a function onEachFeature that calls a handler, in this case it is createPopup.

<GeoJSON data={covidGeoJSON} onEachFeature={createPopup}></GeoJSON>

Within the handler createPopup how can I access states defined inside the CovidMap component?

import React, { useState } from 'react'
import { MapContainer, GeoJSON } from 'react-leaflet'
import 'leaflet/dist/leaflet.css'



CovidMap = ({ covidGeoJSON, styles }) => {

    const showCords = (e) => {
        console.log(e.latlng);
    }

    const createPopup = (state, layer) => {
        layer.on({
            mouseover: showCords,
        });
        layer.bindPopup(state.properties.NAME);
    }

    const [range, setRange] = useState([]);

    return (
        <div>
            <MapContainer style={{ height: '90vh', width: '100vw' }} center={[39.162497380360634, -94.83672007881789]} zoom={5}>
                <GeoJSON data={covidGeoJSON} onEachFeature={createPopup}></GeoJSON>
            </MapContainer>
        </div>
    )
}

export default CovidMap
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should be able to reference the state from createPopup. Create & use state variables within CovidMap using the useState hook:

const CovidMap = () => {

    ...
    const [properties, setProperties] = useState({}); // Defined BEFORE handler
    const [layer, setLayer] = useState({}); // Defined BEFORE handler

    const createPopup = () => {
        layer.on({
            mouseover: showCords,
        });
        layer.bindPopup(properties.NAME);
    }
    ...
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!