mirror of https://github.com/sveltejs/svelte
parent
54d76c97f1
commit
b639581ed7
@ -0,0 +1,30 @@
|
||||
---
|
||||
title: $$props and $$restProps
|
||||
---
|
||||
|
||||
In runes mode, getting an object containing all the props that were passed in is easy, using the [`$props`]($props) rune.
|
||||
|
||||
In legacy mode, we use `$$props` and `$$restProps`:
|
||||
|
||||
- `$$props` contains all the props that were passed in, including ones that are not individually declared with the `export` keyword
|
||||
- `$$restProps` contains all the props that were passed in _except_ the ones that were individually declared
|
||||
|
||||
For example, a `<Button>` component might need to pass along all its props to its own `<button>` element, except the `variant` prop:
|
||||
|
||||
```svelte
|
||||
<script>
|
||||
export let variant;
|
||||
</script>
|
||||
|
||||
<button {...$$restProps} class="variant-{variant} {$$props.class ?? ''}">
|
||||
click me
|
||||
</button>
|
||||
|
||||
<style>
|
||||
.variant-danger {
|
||||
background: red;
|
||||
}
|
||||
</style>
|
||||
```
|
||||
|
||||
In Svelte 3/4 using `$$props` and `$$restProps` creates a modest performance penalty, so they should only be used when needed.
|
||||
@ -1,12 +0,0 @@
|
||||
---
|
||||
title: $$restProps
|
||||
---
|
||||
|
||||
`$$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. It shares the same performance characteristics compared to specific property access as `$$props`.
|
||||
|
||||
```svelte
|
||||
<input {...$$restProps} />
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
> In Svelte 5+, this concept is unnecessary as you can use [`let { foo, ...rest } = $props()`]($props) instead
|
||||
Loading…
Reference in new issue