Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda