Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1 #8

Merged
merged 1 commit into from
Apr 19, 2020
Merged

Fix #1 #8

merged 1 commit into from
Apr 19, 2020

Conversation

mewben
Copy link
Contributor

@mewben mewben commented Feb 6, 2020

this prevents useEffect from running endlessly.

this prevents useEffect from running endlessly.
@codecov-io
Copy link

Codecov Report

Merging #8 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff          @@
##           master     #8   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           2      2           
  Lines          23     23           
  Branches        4      4           
=====================================
  Hits           23     23
Impacted Files Coverage Δ
src/usePosition.js 100% <ø> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1aec1f4...4bccb89. Read the comment docs.

@trekhleb
Copy link
Owner

Thanks for the PR @mewben!

@trekhleb trekhleb merged commit edb645f into trekhleb:master Apr 19, 2020
@techieeliot
Copy link

Hey 👋

@mewben

I'm having this type of usePosition() issue in my react app component. I have the most up to date version, so maybe I'm totally misusing this hook, but I'm not sure how to reduce the number of times that useEffect will call to the API.

Component Snippet

import { usePosition } from 'use-position';
import axios from 'axios';
import UseCurrentPosition from './UseCurrentPosition';
    
const LocalForecast = (props) => {
    const watch = true;
    const { latitude, longitude } =  usePosition(watch, {enableHighAccuracy: true, maximumAge: 300});
    const [ weather, setWeather ] = useState([])
   
    const location = {
       'latitude': `${latitude}`, 
       'longitude': `${longitude}`
    }

    useEffect( () => {
        if(latitude){
            axios.get('http://localhost:8080', {
                params: {
                    lat: latitude, 
                    lon: longitude
                }
            })
            .then((response) => response.data)
            .then((data) => setWeather(data))
            .catch(err => console.log(err));
            console.log(weather)
        }
    }, [location])

console.log repetitions of weather data

Screen Shot 2020-05-13 at 4 43 05 PM

Thanks 🙌

@mewben
Copy link
Contributor Author

mewben commented May 14, 2020

watch: boolean - set it to true to follow the location.

Hi @techieeliot I believe watch=true constantly tracks your location therefore throwing you new values everytime. If you're not interested to follow location changes, try watch=false. :) Hope this helps :)

@techieeliot
Copy link

Hey @mewben,
Thanks for explaining. That change did the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants