Skip to content

Commit a0e0f01

Browse files
authored
Merge pull request #3096 from sveltejs/tweak-docs
tweak docs a bit
2 parents 8431aff + 6e6a675 commit a0e0f01

File tree

2 files changed

+57
-28
lines changed

2 files changed

+57
-28
lines changed

site/content/docs/01-component-format.md

+46-27
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,60 @@ A `<script>` block contains JavaScript that runs when a component instance is cr
2828

2929
---
3030

31-
Svelte uses the `export` keyword to mark a variable declaration as a *property* or *prop*, which means it becomes accessible to consumers of the component:
31+
Svelte uses the `export` keyword to mark a variable declaration as a *property* or *prop*, which means it becomes accessible to consumers of the component (see the section on [attributes and props](docs#Attributes_and_props) for more information).
3232

3333
```html
3434
<script>
35-
// these properties can be set externally
3635
export let foo;
36+
37+
// Values that are passed in as props
38+
// are immediately available
39+
console.log({ foo });
40+
</script>
41+
```
42+
43+
---
44+
45+
You can specify a default value, which will be used if the component's consumer doesn't specify a prop.
46+
47+
In development mode (see the [compiler options](docs#svelte_compile)), a warning will be printed if no default is provided and the consumer does not specify a value. To squelch this warning, ensure that a default is specified, even if it is `undefined`.
48+
49+
```html
50+
<script>
3751
export let bar = 'optional default value';
52+
export let baz = undefined;
53+
</script>
54+
```
55+
56+
---
3857

39-
// you can use export { ... as ... } to have
40-
// props whose names are reserved keywords
41-
let clazz;
42-
export { clazz as class };
58+
If you export a `const`, `class` or `function`, it is readonly from outside the component. Function *expressions* are valid props, however.
4359

44-
// this property is readonly externally
45-
export const buzz = 'buzz';
60+
```html
61+
<script>
62+
// these are readonly
63+
export const thisIs = 'readonly';
4664
47-
// Values that are passed in as props
48-
// are immediately available
49-
console.log(foo, bar);
50-
51-
// Function expressions can also be props
52-
export let format = (number) => (number.toFixed(2));
53-
54-
// Function declarations are added as methods
55-
// on the component, rather than props
56-
export function greetMethod() {
57-
alert(`I'm a <${this.constructor.name}>!`);
65+
export function greet(name) {
66+
alert(`hello ${name}!`);
5867
}
5968
60-
// you can also use export { ... as ... } to have
61-
// methods whose names are reserved keywords
62-
function del() {
63-
do_something();
64-
}
65-
export { del as delete };
69+
// this is a prop
70+
export let format = n => n.toFixed(2);
71+
</script>
72+
```
73+
74+
---
75+
76+
You can use reserved words as prop names.
77+
78+
```html
79+
<script>
80+
let className;
81+
82+
// creates a `class` property, even
83+
// though it is a reserved word
84+
export { className as class };
6685
</script>
6786
```
6887

@@ -81,8 +100,8 @@ Because Svelte's reactivity is based on assignments, using array methods like `.
81100
let count = 0;
82101
83102
function handleClick () {
84-
// calling this function will trigger a re-render
85-
// if the markup references `count`
103+
// calling this function will trigger an
104+
// update if the markup references `count`
86105
count = count + 1;
87106
}
88107
</script>

site/content/docs/02-template-syntax.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ A lowercase tag, like `<div>`, denotes a regular HTML element. A capitalised tag
2020
```
2121

2222

23-
### Attributes
23+
### Attributes and props
2424

2525
---
2626

@@ -76,6 +76,16 @@ When the attribute name and value match (`name={name}`), they can be replaced wi
7676

7777
---
7878

79+
By convention, values passed to components are referred to as *properties* or *props* rather than *attributes*, which are a feature of the DOM.
80+
81+
As with elements, `name={name}` can be replaced with the `{name}` shorthand.
82+
83+
```html
84+
<Widget foo={bar} answer={42} text="hello"/>
85+
```
86+
87+
---
88+
7989
*Spread attributes* allow many attributes or properties to be passed to an element or component at once.
8090

8191
An element or component can have multiple spread attributes, interspersed with regular ones.

0 commit comments

Comments
 (0)