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.

SvelteKit - Function called outside component initialization

See original GitHub issue

Hi

I’m trying to use sswr with a newly created SvelteKit project, building with the node adapter. I have added some simple test after having had issues:

	import { useSWR } from 'sswr';

	try {
		const { data } = useSWR('http://jsonplaceholder.typicode.com/posts/1');
	} catch(e) {
		console.log(e);
	}

test to a

  • route
  • component

and I always get back the same error:

Error: Function called outside component initialization
    at get_current_component (index.mjs:813)
    at onMount (index.mjs:820)
    at l.useSvelte (index.js:1)
    at useSWR (index.js:1)
    at instance ([dialogId].svelte:42)
    at init (index.mjs?v=99bae134:1660)
    at new U5BdialogIdu5D ([dialogId].svelte:153)
    at createComponent (svelte-hooks.js:136)
    at U5BdialogIdu5D.targetCmp.$replace (svelte-hooks.js:183)
    at refreshComponent (proxy.js:170)

Any idea why that might be?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
benbendercommented, Aug 9, 2021

It’s not a bug in this package, but in sveltekit. I saw the same problem on my own tinkering with react-query - so I opened a bug in sveltekit. See https://github.com/sveltejs/kit/issues/2147.

Btw, it only happens when both preconditions are true: a) lifecycle-method called inside an installed package. b) in dev-mode.

It works in svelte-repl though. So its likely a problem with the vite-dev-server.

PS: You can circumvent the bug for now by installing swrev and copy sswr to your local $lib-folder and import it from there.

1reaction
ConsoleTVscommented, Aug 10, 2021

It’s not a bug in this package, but in sveltekit. I saw the same problem on my own tinkering with react-query - so I opened a bug in sveltekit. See https://github.com/sveltejs/kit/issues/2147.

Btw, it only happens when both preconditions are true: a) lifecycle-method called inside an installed package. b) in dev-mode.

It works in svelte-repl though. So its likely a problem with the vite-dev-server.

PS: You can circumvent the bug for now by installing swrev and copy sswr to your local $lib-folder and import it from there.

That could explain why my local symlinked pkg was working lol

Read more comments on GitHub >

github_iconTop Results From Across the Web

Function called outside component initialization - Stack Overflow
i wanna to recall the onMount in my Svelte app by clicking some button, but i got this error. Any idea would be...
Read more >
Function called outside component initialization #592 - GitHub
I'm getting a Function called outside component initialization error from a sapper v0.26.0-alpha.10 project. A component is being loaded ...
Read more >
Svelte - Function called outside component initialization from ...
Hi there, I have got a very weird behaviour when I'm creating new Sveltekit app. As explained in the doc…
Read more >
How to fix "Function called outside component initialization ...
But when I moved svelte to a dependency I got an error saying Function called outside component initialization . This is documented on...
Read more >
Function called outside component initialization - CodeSandbox
urql svelte, error: Function called outside component initialization. 0. Embed Fork Create Sandbox Sign in. Sandbox Info. urql svelte, error: Function ...
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