Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I receive arbitrary props in a Svelte component and pass to a child component?

I want to receive arbitrary props from "above" and spread them onto an <input>, as shown here where inputProps would become an object containing any additional props set on this component (similar to python's **kwargs, in case you're familiar):

<script>
export let id;
export ...inputProps;
</script>

<div>
    id: {id}
    <input {...inputProps} />
</div>

Can you point me toward the correct Svelte mechanism for accomplishing something like this? I have a feeling that I'm asking the wrong question, but I need a svelte developer to set me straight. Should I use a slot instead? Or learn about actions / "the use directive"?

like image 376
colllin Avatar asked Dec 17 '19 04:12

colllin


People also ask

How do you pass component as prop in Svelte?

The solution. To pass Svelte components dynamically down to a child component you have to use Svelte's <svelte:component> directive. <svelte:component> accepts a property called this . If the property this has a component attach to it than it will render the given Svelte component.

What is $: In Svelte?

In a Svelte component, prefixing a statement with $: marks the statement as reactive — it will run whenever the variables referenced in that statement change. Here's a classic example. Whenever num changes, doubled is automatically set to the correct value.

How do I export component Svelte?

Exporting a Svelte component </h1> ); export default GreetMessage; Once you've created your class or functional React component, you must use the export keyword or you can use export default to export that React component. In Svelte, by default it's exported as soon you create your Svelte file!

How do you declare a variable in Svelte?

Svelte uses the export keyword to mark a variable declaration as a property or prop, which means it becomes accessible to consumers of the component (see the section on attributes and props for more information). You can specify a default initial value for a prop.


Video Answer


3 Answers

Svelte now also offers $$restProps. See the Svelte API Docs - Attributes and props.

$$props references all props that are passed to a component – including ones that are not declared with export. It is useful in rare cases, but not generally recommended, as it is difficult for Svelte to optimise.

<Widget {...$$props}/>

$$restProps contains only the props which are not declared with export. It can be used to pass down other unknown attributes to an element in a component.

<input {...$$restProps}>
like image 195
colllin Avatar answered Oct 10 '22 01:10

colllin


You can use $$props to access all the props given to a component.

$$props references all props that are passed to a component – including ones that are not declared with export. It is useful in rare cases, but not generally recommended, as it is difficult for Svelte to optimise.

Example (REPL)

<!-- App.svelte -->
<script>
  import Child from './Child.svelte';
</script>

<Child id="foo" placeholder="bar" />

<!-- Child.svelte -->
<script>
  let id, inputProps;
  $: ({ id, ...inputProps } = $$props);
</script>

<div>
  id: {id}
  <input {...inputProps} />
</div>
like image 8
Tholle Avatar answered Oct 10 '22 00:10

Tholle


While exporting you don't need to use the spread operator

<script>
 export let id;
 export inputProps;
</script>

<div>
 id: {id}
 <input {...inputProps} />
</div>
like image 1
RajuPedda Avatar answered Oct 10 '22 01:10

RajuPedda