question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dependent requests does not seem to be reactive

See original GitHub issue

Describe the bug In my NextJS application, I wanted to use the useFetch hook to fetch some data from the API. I needed to grab some URL parameter from the router. As this parameter was undefined during the first render cycle, I tried to set up some dependent request like it is shown in the documentation.

const DetailPage: NextPageWithLayout<DetailPageProps> = (props) => {
	const router = useRouter();
	const { postId } = router.query;

	const { data, loading, error } = useFetch(getPost.setParams({ postId }), {
		disabled: !postId
	});

	...

	return <div>...</div>;
}

Unfortunately the command I passed to useFetch was never executed, once the postId was ready.

To Reproduce I have reproduced this behaviour in the following CodeSandbox: https://codesandbox.io/s/hyper-fetch-usefetch-disabled-not-reactive-c4zfi8?file=/src/App.js

There is some isFetchPostsDisabled property that is set to false after 1 second, but the command does not get executed. Maybe I am misunderstanding something?

I needed to work around this issue by creating some wrapper component, that I only include once the isFetchPostsDisabled is set to false, but this is not a real dependent request anymore. https://codesandbox.io/s/hyper-fetch-usefetch-disabled-not-reactive-fixed-using-a-middleware-component-7mq6bj?file=/src/App.js

Expected behavior Once the “disabled” property turns to false, I expect the command to be executed.

I was hoping you could help me a bit here. Maybe I am misunderstanding something?

Regards, Stefan

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
stefanullingercommented, Oct 17, 2022

Nice, works perfectly now! Thanks a lot!

0reactions
github-actions[bot]commented, Oct 15, 2022

🎉 This issue has been resolved in version 1.6.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reactive statement re-run even though dependency (exported ...
Because the $: items = fetchItems(filters) block depends only on filters and filters has not changed, it should not be re-run.
Read more >
How to Prevent Reactive Java Applications from Stalling
Some studies suggest that performance differences are negligible in Spring applications unless the application makes more than 500 API requests ...
Read more >
Should we use a reactive-stack web framework like spring ...
But a rest call isn't blocking, as it is not thread dependent unless you use thread dependent functionality, like ThreadLocal which spring ...
Read more >
Notes on Reactive Programming Part III: A Simple HTTP ...
We have an HTTP server with (probably) non-blocking IO handlers, passing work back to a thread pool, one HTTP request per thread —...
Read more >
Understanding common frustrations with React Hooks
React Hooks can be frustrating despite their popularity and widespread use. Learn about some of the drawbacks to using React Hooks.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found