Code Monkey home page Code Monkey logo

Comments (15)

alex-cory avatar alex-cory commented on May 18, 2024 2

Try passing response into useEffect

from use-http.

alex-cory avatar alex-cory commented on May 18, 2024 1

@rhobot I need to take another look. Thanks for the heads up!

from use-http.

alex-cory avatar alex-cory commented on May 18, 2024 1

I will get to this as soon as possible. Trying to finish up retryOn and retryDelay real quick

from use-http.

alex-cory avatar alex-cory commented on May 18, 2024 1

@rhobot This should be working correctly in v0.4.5

from use-http.

alex-cory avatar alex-cory commented on May 18, 2024

Hey! Sorry for the delay. The examples are laid out for simplicity of the beginner, but technically, you should listen to the linting rules. Wrap initializeTodos in useCallback.

from use-http.

alex-cory avatar alex-cory commented on May 18, 2024

Also, thanks so much for your support! I love this thing too! That's why I made it lol

from use-http.

alex-cory avatar alex-cory commented on May 18, 2024

@tobyl is you question answered? Gonna close this if it is :)

from use-http.

tobyl avatar tobyl commented on May 18, 2024

I'm sorry - I didn't get the notifications for your msgs. Yes, this is super-helpful - I'll take your advice - thank you so much!

from use-http.

alex-cory avatar alex-cory commented on May 18, 2024

If you're having infinite loop problems, either destructure your request object and use the method as a dependency like:

const { get, loading } = useFetch(...)

const initializeTodos = useCallback(async () => {
  ...
}, [get])

or you could do this for on mount

const mounted = useRef(false)
useEffect(() => {
  if (!mounted.current) {
    initializeTodos()
    mounted.current = true
  }
})

from use-http.

tobyl avatar tobyl commented on May 18, 2024

Those look like two great options - I'll play around with it, thanks again!

from use-http.

rhobot avatar rhobot commented on May 18, 2024

I have a related question. The lint warns that I don't add response.ok. But I believe response.ok should not be added as a dependency. Otherwise, it triggers useEffect again:

useEffect(() => {
  async function fetchSomething() {
    const data = await get(...)

    if (response.ok) {
       // ...
    }
  }

  fetchSomething()
}, [get]) // <-- eslint warns that response.ok is not added to dependency

I'm eslint-disable-line it for now. Is there a suggestion on handling this lint warning?

from use-http.

alex-cory avatar alex-cory commented on May 18, 2024

I haven't had time to try this out yet, but based off of the NOTE portion of the use-deep-compare-effect docs I bet if we wrapped response in useMemo(() => response, []) it might not cause the infinite loop.

from use-http.

rhobot avatar rhobot commented on May 18, 2024

That technically works but then eslint will warn that response is missing useMemo's dependencies.

I've also tried with uesRef but had no luck.

from use-http.

tayden avatar tayden commented on May 18, 2024

I'm still getting linter suggestions that response.ok and get should be in my useEffect dependency list. But adding response.ok to the list causes re-fetching on each render.

Is this the expected behavior? What's the correct way to deal with this?

I'm currently just excluding response.ok from the dependencies, and ignoring the lint errors.

Using use-http version 1.0.16

from use-http.

iqoOopi avatar iqoOopi commented on May 18, 2024

Try passing response into useEffect

Hi @alex-cory , got the same question. I'm wondering why adding response to useEffect/useCallback dependency won't cause re-render?

Let's say I have three api calls inside the componet

 const { loading, response, get, put } = useFetch();
  const call1= useCallback(async () => {

    const data= await get('endpoint1');
    if (response.ok) {
         
    }
  }, [get, response]);

  const call2= useCallback(async () => {

    const data= await get('endpoint2');
    if (response.ok) {
         
    }
  }, [get, response]);

  useEffect(() => {
    call2();
  }, [call2]);

when Call1 fired, the response will be changing, thus will cause a re-render on call2 as well?

from use-http.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.