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

Why I'm getting 2 rendered using this hook, when component mounts? #1061

Closed
gremo opened this issue Sep 29, 2022 · 9 comments
Closed

Why I'm getting 2 rendered using this hook, when component mounts? #1061

gremo opened this issue Sep 29, 2022 · 9 comments
Labels

Comments

@gremo
Copy link

gremo commented Sep 29, 2022

Plain simple:

import React from 'react';
import useAxios from 'axios-hooks';

export const Products = () => {
  const [{ data, loading, error }] = useAxios({
    url: 'https://dummyjson.com/products',
  }, { useCache: false });

  return (<div></div>);
}

The profiler shows 3 renders: first one it's the initial rendering. Second and third are from "Hook 8 changed". Why?

image

Hook 8 is:

image

@simoneb
Copy link
Owner

simoneb commented Sep 29, 2022

because react 18 renders twice in development mode. https://stackoverflow.com/questions/72238175/useeffect-is-running-twice-on-mount-in-react

@simoneb simoneb closed this as not planned Won't fix, can't repro, duplicate, stale Sep 29, 2022
@gremo
Copy link
Author

gremo commented Sep 29, 2022

@simoneb not sure about that. I use for testing another hook (use-http):

const { loading, error, data } = useFetch('https://dummyjson.com/products', {}, []);

Why this hook only trigger 1 render? Two in total...

@simoneb
Copy link
Owner

simoneb commented Sep 29, 2022

can you provide a repro?

@gremo
Copy link
Author

gremo commented Sep 29, 2022

@simoneb I really appreciate your work, and OS software in general. For me, and maybe for you, it's more than a work. So I suggest you not to close an issue after five minutes, making assumptions about other's project/setup.

Yes, I maybe a beginner in React but I'm not completely dumb.

  1. Yes, I'm using React 18 (but you didn't know)
  2. I'm not using strict mode, neither in that component nor up the tree (but you didn't know)
  3. Either way, I would have noticed already the double rendering, elsewhere in my app
  • 3 rendering using useAxios sandbox

useAxios

  • 2 rendering using useFetch sandbox

useFetch

@simoneb simoneb reopened this Sep 29, 2022
@simoneb
Copy link
Owner

simoneb commented Sep 29, 2022

I see your point, I had assumed React 18 was the cause. I've had a look at your repro and it looks legit. This seems to be causes by an improper handling of the state of the initial request.

Would you be interested in submitting a PR to fix this?

@gremo
Copy link
Author

gremo commented Sep 29, 2022

@simoneb thanks but I really can't, my experience with React isn't advanced yet! Hope to see a fix soon (or revert to a previous version maybe?)

@gremo
Copy link
Author

gremo commented Oct 7, 2022

Any update @simoneb ?

@simoneb
Copy link
Owner

simoneb commented Oct 7, 2022

@gremo I didn't have a chance to look into it, but why don't you give it a go? I think it might be pretty simple. If the new state object is the same as the existing, don't change the state :)

@Paola3stefania
Copy link

Hello ! For what we have been researching with @Cherrerotinoco It has to be with how the initialState its hanlded with manual false (automatic ), you can prevent this refetching error with (manual: true) while waiting for a new PR

@github-actions github-actions bot added the Stale label Dec 9, 2022
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 17, 2022
@simoneb simoneb reopened this Dec 19, 2022
@simoneb simoneb removed the Stale label Dec 19, 2022
@github-actions github-actions bot added the Stale label Feb 18, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 25, 2023
@simoneb simoneb removed the Stale label Feb 25, 2023
@simoneb simoneb reopened this Feb 25, 2023
@github-actions github-actions bot added the Stale label Apr 27, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 5, 2023
@simoneb simoneb removed the Stale label May 5, 2023
@simoneb simoneb reopened this May 5, 2023
@github-actions github-actions bot added the Stale label Jul 5, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 13, 2023
@simoneb simoneb reopened this Jul 17, 2023
@simoneb simoneb removed the Stale label Jul 17, 2023
@github-actions github-actions bot added the Stale label Sep 16, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants