Signature
The fallback value to use when the parent component doesn’t bind to this prop.
Mutation is also possible with normal props, but is strongly discouraged — Svelte will warn you if it detects that a component is mutating state it does not ‘own’.
Basic usage
To mark a prop as bindable, we use the$bindable rune:
<FancyInput> can add the bind: directive:
The parent component doesn’t have to use
bind: — it can just pass a normal prop. Some parents don’t want to listen to what their children have to say.Fallback values
In this case, you can specify a fallback value for when no prop is passed at all:Two-way binding
When a parent component binds to a child prop usingbind:value={message}, changes in the child will flow back up to the parent. This creates a two-way binding:
- Parent changes update the child
- Child changes update the parent
- Parent
- Child
When to use bindable props
Use$bindable sparingly. It’s most appropriate for:
- Form input wrappers (like the
FancyInputexample) - Tightly coupled components where bidirectional data flow is natural
- Creating reusable components that need to update parent state
- Passing data down via props
- Passing callbacks up via event handler props
- Using context for deeply nested components