Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
Svelte keep default prop value of a child component

I have component1 that takes let text as a prop and then component2 that does almost the same thing, but I'd like to keep component1 separate for better reusability.

So I wrapped the comp1 (Child.svelte) with comp2 (Wrapper.svelte). But how do I keep the default prop value of the Child component without writing it again?

here is an example:

//Wrapper.svelte
<script lang="ts">
    import Child from "./Child.svelte";
    export let text = 'hello world'; //need to type the default value again
</script>

<Child text={text} />
//Child.svelte
<script lang="ts">
    export let text = 'hello world';
</script>

<p>{text}</p>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Thank you @hackape and @Stephane Vanraes for your answers!

the answer from @hackape seems like the answer I was looking for, but It still throws this typescript error: Property 'text' is missing in type '{}' but required in type '{ text: string; }'.ts(2322) when I don't provide any value to Wrapper comp. from the outside.

I should have realised this earlier, but I combined both answers and came up with this:

//Wrapper.svelte
<script lang="ts">
    import Child from "./Child.svelte";
    export let text: string = undefined;
</script>

<Child bind:text />

also works with <Child text={text}/>

I am relatively new to Stack Overflow, should I accept my or @hackapes answer?

about 4 years ago · Juan Pablo Isaza Report

0

Use bind:prop to create a two way binding. Docs: https://svelte.dev/tutorial/component-bindings

//Wrapper.svelte
<script lang="ts">
import Child from "./Child.svelte";
export let text: string
</script>

<Child bind:text />
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!