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

234
Views
How to destructure into global variables from inner function? (ReactJS)

I have a react functional-based component that needs some pieces of information lat and lng to pass into my Autocomplete component. I am trying to get lat and lng from an inner function but i couldnt. I tried doing

{ lat, lng } = getLatLng(results[0]) 

but it throws some syntax error, so I also tried

lat = getLatLng(results[0].lat)
lng = getLatLng(results[0].lng)

but it doesnt seem to work. This seems like such a simple issue but i cant figure out why. I tried putting the var keyword in front as well but it didnt work, nor did i expect it to. Below is my whole code for the component.

import usePlacesAutocomplete, { getGeocode, getLatLng } from "use-places-autocomplete"
import { useDispatch } from "react-redux";
import { setOrigin, setDestination } from '../../Slices/originDestinationSlice'
import TextField from '@mui/material/TextField';
import Autocomplete from '@mui/material/Autocomplete';

export const StartPlaces = () => {
    const dispatch = useDispatch()
    const { ready, value, setValue, suggestions: { status, data }, clearSuggestions } = usePlacesAutocomplete();

    const handleSelect = async (val, selectedValue) => {
        setValue(val, true);
        // clearSuggestions();
        const results = await getGeocode({ address: val });

        // { lat, lng } = getLatLng(results[0])
        lat = getLatLng(results[0].lat)
        lng = getLatLng(results[0].lng)

    }
    return <>
        <h4>Enter Origin</h4>
        <Autocomplete
            id="free-solo-demo"
            freeSolo
            options={data.map(({ description }) => description)}
            onChange={(event, value) => {
                
                dispatch(setOrigin({
                    coordinates: { lat, lng },
                    name: value
                }))
            }}
            renderInput={(params) => <TextField {...params} label="Origin" onChange={(e) => handleSelect(e.target.value)} placeholder="e.g. Las Vegas" />}
            sx={{ width: 300 }}
        />
    </>
}

Any help would be greatly appreciated. thank you in advance!!

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

0

you should save your lat e lng in a state to use them correctly

try to change your code like this

import usePlacesAutocomplete, { getGeocode, getLatLng } from "use-places-autocomplete"
import React, {useState} from 'react'
import { useDispatch } from "react-redux";
import { setOrigin, setDestination } from '../../Slices/originDestinationSlice'
import TextField from '@mui/material/TextField';
import Autocomplete from '@mui/material/Autocomplete';

export const StartPlaces = () => {
    const dispatch = useDispatch()
    const { ready, value, setValue, suggestions: { status, data }, clearSuggestions } = usePlacesAutocomplete();
    const [lat, setLat] = useState(0)
    const [lng, setLng] = useState(0)
    const handleSelect = async (val, selectedValue) => {
        setValue(val, true);
        // clearSuggestions();
        const results = await getGeocode({ address: val });

        
        const {lat, lng} = getLatLng(results[0])
        setLat(lat)
        setLng(lng)

    }
    return <>
        <h4>Enter Origin</h4>
        <Autocomplete
            id="free-solo-demo"
            freeSolo
            options={data.map(({ description }) => description)}
            onChange={(event, value) => {
                
                dispatch(setOrigin({
                    coordinates: { lat, lng },
                    name: value
                }))
            }}
            renderInput={(params) => <TextField {...params} label="Origin" onChange={(e) => handleSelect(e.target.value)} placeholder="e.g. Las Vegas" />}
            sx={{ width: 300 }}
        />
    </>
}
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!