Svelte provides single file components. Every component is declared into a .svelte file, and in there you can write the HTML markup, the CSS and the JavaScript needed.

You can import a separate component using the syntax import ComponentName from 'componentPath':

<script>
import SignupForm from './SignupForm.svelte';
</script>

Once you do so, you can use the newly imported component in the markup, like an HTML tag:

<SignupForm />

In this way you are forming a parent/child relationship between the two components: the one that imports, and the one that is imported.

Often you want to have the parent component pass data to the child component.

You can do so using props. Props behave similarly to attributes in plain HTML, and they are a one-way form of communication.

In this example we pass the disabled prop, passing the JavaScript value true to it:

<SignupForm disabled={true}/>

In the SignupForm component, you need to export the disabled prop, in this way:

<script>
  export let disabled
</script>

This is the way you express the fact that the prop is exposed to parent components.

When using the component, you can pass a variable instead of a value, to change it dynamically:

<script>
import SignupForm from './SignupForm.svelte';
let disabled = true
</script>

<SignupForm disabled={disabled}/>

When the disabled variable value changes, the child component will be updated with the new prop value. Example:

<script>
import SignupForm from './SignupForm.svelte';
let disabled = true
setTimeout(() => { disabled = false }, 2000)
</script>

<SignupForm disabled={disabled}/>

Found a typo or problem? Edit this page