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

206
Views
I can't access html element inside useEffect

I want to generate itinerary control (table element) generated in <RoutingMachine />. But I cannot access html element in useEffect because it still not exist.

const Routing = ({ pointA, pointB }) => {

    const dispatch = useDispatch()

    const createLayer = () => {
        const instance = L.Routing.control({
            waypoints: [
                L.latLng(pointA.position.lat, pointA.position.lng),
                L.latLng(pointB.position.lat, pointB.position.lng)
            ],
            lineOptions: {
                styles: [{ color: "red", weight: 4 }]
            },
        });

        return instance;
    };

    const RoutingMachine = createControlComponent(createLayer);

    useEffect(() => {
        let routeInfo = document.querySelectorAll('tr')
        console.log(routeInfo) // array with 0 elements
    }, []);

    return <RoutingMachine />

};
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Can you try this (with useRef()):

const Routing = ({ pointA, pointB }) => {

    const dispatch = useDispatch()
    const mounted = useRef(true)

    const createLayer = () => {
        const instance = L.Routing.control({
            waypoints: [
                L.latLng(pointA.position.lat, pointA.position.lng),
                L.latLng(pointB.position.lat, pointB.position.lng)
            ],
            lineOptions: {
                styles: [{ color: "red", weight: 4 }]
            },
        });

        return instance;
    };

    const RoutingMachine = createControlComponent(createLayer);

    useEffect(() => {
        if (mounted.current) {
          let routeInfo = document.querySelectorAll('tr')
          console.log(routeInfo) // array with 0 elements
        }

        return () => {
          mounted.current = false;
        }
    }, [mounted]);

    return <RoutingMachine />

};
about 4 years ago · Juan Pablo Isaza Report

0

You can use references supported by leaflet, checkout this Reference

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!