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

Reactive statements throwing TypeError from svelte plugin #2510

Closed
reinink opened this issue Apr 23, 2019 · 4 comments · Fixed by #2518
Closed

Reactive statements throwing TypeError from svelte plugin #2510

reinink opened this issue Apr 23, 2019 · 4 comments · Fixed by #2518
Assignees
Labels

Comments

@reinink
Copy link

reinink commented Apr 23, 2019

I just ran into an issue with Svelte with the reactive statements. I created a sample app using the sveltejs/template starter repo. I added a single reactive statement to the App.svelte component, which you can see here

<script>
	export let name;

	$: document.title = name;
</script>

<style>
	h1 {
		color: purple;
	}
</style>

<h1>Hello {name}!</h1>

Here is the error I'm getting:

1!] (svelte plugin) TypeError: extractors[param.type] is not a function
npm ERR! code ELIFECYCLE
npm ERR! errno 1ctors[param.type] is not a function
npm ERR! svelte-app@1.0.0 autobuild: `rollup -c -w`y-svelte-project/node_modules/svelte/compiler.js:17623:28)
npm ERR! Exit status 1/Users/jonathan/Sites/my-svelte-project/node_modules/svelte/compiler.js:17619:13)
npm ERR! ript.content.body.forEach.node (/Users/jonathan/Sites/my-svelte-project/node_modules/svelte/compiler.js:22946:14)
npm ERR! Failed at the svelte-app@1.0.0 autobuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.es/svelte/compiler.js:22938:30)
    at new Component (/Users/jonathan/Sites/my-svelte-project/node_modules/svelte/compiler.js:22594:15)
npm ERR! A complete log of this run can be found in:ect/node_modules/svelte/compiler.js:23614:24)
npm ERR!     /Users/jonathan/.npm/_logs/2019-04-23T13_46_58_987Z-debug.logt/node_modules/rollup-plugin-svelte/index.js:236:22)
ERROR: "autobuild" exited with 1.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! svelte-app@1.0.0 dev: `run-p start:dev autobuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the svelte-app@1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Full repo here.

@kazzkiq
Copy link

kazzkiq commented Apr 23, 2019

I'm guessing you're trying to set your page's title from within your component, whenever name variable changes? In this case you could trigger reactive assignments to change your title as follows:

<script>
  export let name;

  $: if (name) {
    document.title = name;
  }
</script>

<h1>Hello {name}!</h1>

If you just need to set it once in your component's load, then use onMount:

<script>
  import { onMount } from 'svelte';

  export let name;

  onMount(() => {
    if (name) document.title = name;
  });
</script>

<h1>Hello {name}!</h1>

@reinink
Copy link
Author

reinink commented Apr 23, 2019

That worked! I literally just went off the API docs for this. Do the docs need updating?

<script>
	export let title;

	// this will update `document.title` whenever
	// the `title` prop changes
	$: document.title = title;

	$: {
		console.log(`multiple statements can be combined`);
		console.log(`the current title is ${title}`);
	}
</script>

image

@kazzkiq
Copy link

kazzkiq commented Apr 23, 2019

Hmm, I think you caught a bug.

This works:

<script>
  export let name;

  $: {
    document.title = name;
  }
</script>

This should work, but it's raising the error you mentioned:

<script>
  export let name;

  $: document.title = name;
</script>

Both are valid as per docs.

@vovs03
Copy link

vovs03 commented Apr 23, 2019

#2514

@Conduitry Conduitry added the bug label Apr 23, 2019
@Conduitry Conduitry self-assigned this Apr 23, 2019
Rich-Harris added a commit that referenced this issue Apr 25, 2019
 handle unknown nodes in extract_identifiers
pixeline added a commit to pixeline/svelte that referenced this issue Jan 5, 2020
The previous statement does not work (see sveltejs#2510)
I'm not able to fix it, so instead, let's just update the documentation :-)
pixeline added a commit to pixeline/svelte that referenced this issue Jan 5, 2020
The previous statement does not work (see sveltejs#2510)
I'm not able to fix it, so instead, let's just update the documentation :-)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants