Search code examples
sveltesvelte-component

Props value passing style differences in Svelte components


I saw many styles of passing a prop value in a svelte components like,

  1. <MyComponent color={"#b291ff7a"} />
  2. <MyComponent color="#b291ff7a" />
  3. <MyComponent color={value} />
  4. <MyComponent color="{value}" />

What are their differences and when should I use which one?

Just asking for clarifiction


Solution

  • This are the main differences

    <MyComponent color={"#b291ff7a"} />

    • value is passed as string.

    <MyComponent color="#b291ff7a" />

    • value is passed as string also, but the component might not receive it as expected. So it is not recommended.

    <MyComponent color={value} />

    • passing the value variable as it is.

    <MyComponent color="{value}" />

    • passing the value of the variable as string not matter what it is.