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

235
Views
svelte prop value not working with if statement

this is my svelte component code

<script>
    export let canCascade = true;
    let show = true;

    function cascade() {
        if (canCascade) {
            show = !show;
        }
    }
</script>

{#if show}
<div class="shade" on:click|self={cascade}>
    shade
</div>
{/if}

When I use the component as <Component canCascade=false /> the 'if block' doesn't work. But hard-coding the value inside just works fine. Am I missing something here - some conceptual error?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Like @Corrl pointed out in the comments, you need to use {brackets}. If you don't, the variable will follow the rules of an html attribute.

Working repl https://svelte.dev/repl/d13df678eab243e9a13fb705da197219?version=3

about 4 years ago · Juan Pablo Isaza Report

0

In Svelte when we want to pass JavaScript value / expression to an attribute of a component we need to wrap the value / expression with curly brackets {}.

Otherwise, it will be used as a string.

As an example, take a look at the following code:

Component.svelte:

<script>
  export let test = true;

  $test: console.log(`typeof test = ${typeof test}`);
</script>

App.svelte:

<script>
  import Component from "./Component.svelte";
</script>

<Component test=true />
<div />

When you will open the console inside the browser developer tools, the output will be:

typeof test = string

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!