-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: clarify default prop values, attribute behavior and text expressions #15257
base: main
Are you sure you want to change the base?
Changes from 1 commit
d3d95fb
3fac864
e5e675d
cf30a6e
96a39e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,24 +177,30 @@ A JavaScript expression can be included as text by surrounding it with curly bra | |
{expression} | ||
``` | ||
|
||
When using {expression} inside markup, Svelte automatically converts the value to a string before rendering it. The conversion follows JavaScript's standard behavior: | ||
When using {expression} inside markup, Svelte automatically converts the value to a string before rendering it and makes the expression reactive (similar to wrapping it in $derived). The conversion follows JavaScript's standard behavior: | ||
|
||
- Primitive values (number, boolean, string) are directly converted to strings. | ||
- Objects call their .toString() method (if not overridden, it defaults to [object Object]). | ||
- undefined and null are treated as empty strings (""). | ||
- Undefined and null are treated as empty strings (""). | ||
- Expressions using runes ($state, $derived, etc.) maintain their specific reactive behavior. | ||
|
||
```svelte | ||
<script> | ||
let emptyStr = ""; | ||
let num = 1; | ||
let bool = false; | ||
let obj = { key: "value" }; | ||
let objToStr = obj.toString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant this: <script lang="ts">
let obj1 = { toString: () => "str" };
</script>
<p>{obj1}</p> <!-- Renders as: <p>str</p> --> And add a link to MDN. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, ok. My bad. So you want me to override the toString function right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add this instead of objToStr. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should i add the MDN link to documents? Or you just share with me for the reference? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now let's wait for what the team says. |
||
let empty = undefined; | ||
</script> | ||
let nul = null; | ||
|
||
|
||
<p>{emptyStr}</p> <!-- Renders as: <p></p> --> | ||
<p>{num}</p> <!-- Renders as: <p>1</p> --> | ||
<p>{bool}</p> <!-- Renders as: <p>false</p> --> | ||
<p>{obj}</p> <!-- Renders as: <p>[object Object]</p> --> | ||
<p>{objToStr}</p> <!-- Renders as: <p>[object Object]</p> --> | ||
<p>{empty}</p> <!-- Renders as: <p></p> (empty string) --> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add an example with an empty string, null, and an object with the toString method. |
||
<p>{nul}</p> <!-- Renders as: <p></p> --> | ||
``` | ||
|
||
Curly braces can be included in a Svelte template by using their [HTML entity](https://developer.mozilla.org/docs/Glossary/Entity) strings: `{`, `{`, or `{` for `{` and `}`, `}`, or `}` for `}`. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, I didn't make myself clear. I meant deep destructuring assignment, but it turns out that it is forbidden.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should i remove the example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think it's unnecessary here.