'Svelte alias/rename props
Is there any way to rename/alias props in svelte?
For example, if I have a component which takes a foo prop but I also want a foo local variable for the current state, is there any way to rename the incoming prop a bit like this:
export let foo as forceFoo;
let foo = forceFoo | null;
Normally the correct answer is one of these two:
- Rename the prop to something like
initialFoo - Rename the state
Renaming the prop is not appropriate in this case - it's the public API of the component and it's not an initial state, it's an optional override that forces the value of that field.
Renaming the state is ok for a single field and usually works well for generic components, but becomes horrible and unwieldy when the component is a form with many fields and has to pass those fields on to a save function that expects them to have the right names.
Solution 1:[1]
The proper syntax is the following:
<script>
// aliased prop
let forceFoo
export { forceFoo as foo }
// local state
let foo = forceFoo
</script>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rixo |
