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

Data not populated at mount #368

Open
maximelebastard opened this issue May 16, 2022 · 5 comments
Open

Data not populated at mount #368

maximelebastard opened this issue May 16, 2022 · 5 comments

Comments

@maximelebastard
Copy link

Describe the bug
When using the provided example, no data is populated into the state.

To Reproduce
Use that example

import { useFetch } from "use-http";

function Data() {
  const {
    loading,
    error,
    data = [],
  } = useFetch(
    "https://myserver.com/?query=toto",
    {},
    []
  );

  return (
    <>
      {JSON.stringify(data, error, loading)}
    </>
  );
}

export default Data;

Expected behavior
The data appears into the data attribute

Actual behavior
The request is executed and visible into the Network tab of the Chrome Dev Tools, but no data is put in the state.

@MuzafferDede
Copy link

MuzafferDede commented May 17, 2022

+1

@maximelebastard Did you figure out anything?

@maximelebastard
Copy link
Author

@MuzafferDede Nope sorry, I ended up switching to axios-hooks
https://www.npmjs.com/package/axios-hooks

@MuzafferDede
Copy link

@maximelebastard I figured out, It is because of React 18's strict mode. If you turn off, it will be fine or you can rollback to React 17 until this is handled by use-http package. Some other packages also faced this.

@krishnasaga
Copy link
Contributor

krishnasaga commented Dec 3, 2022

It is happening due to abort on unmount feature useEffect(() => request.abort, []) at

useEffect(() => request.abort, [])

If I remove this line it is working, but it is making duplicate get requests as component mounts, unmounts and remounts again in React 18 StrictMode.

Now I'm trying to solve this problem with abort the requests on unmount and making it work in React StrictMode.

Please let me know if you have any inputs

@alex-cory

@vicmortelmans
Copy link

I suppose this is the same issue that I see here.

An extract from my functional component:

    const [date, setDate] = useState(formatDate(nextDate(0), 'yyyy-MM-dd'))  // next Sunday
    const [jsonData, setJsonData] = useState({});

    // initializing Http API for fetching data from database
    const {loading, error, data} = useFetch(
        `https://${window.location.hostname}${proxy}/query?date=${date}`, 
        {responseType: 'json'}, 
        [date]
    );
    useEffect(() => {
        if (data) {
            setJsonData(data)
        }
    }, [data])

So I want to execute the effect when data becomes available. UseFetch is run when the component mounts, I can see the response coming back in the console, but the effect is not running. When I do setDate(), useFetch runs again on the new url, and then the effect is executed correctly!

Best regards,
Vic

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

No branches or pull requests

4 participants