svelte logic (if else) - can you write this in the js? (potential very dumb question)

Basically I want to put a conditional in here so if count is == 0 then the decrement button vanishes (haven’t coded it as on phone).. I figured that’d be a conditional in the decrement function, and if it is do I write it as a typical js conditional, or a svelte conditional? ‘’’js <script> let count = 0; function increment() { count += 1; } </script> <button on:click={increment}> Clicked {count} {count === 1 ? 'time' : 'times'} </button> {#if count > 10} <p>{count} is greater than 10</p> {:else} <p>{count} is between 0 and 10</p> {/if}’’’js
5 Replies
vince
vince6mo ago
I've never worked with svelte but if it's like a templating engine then I would put the conditional to hide the button in svelte Looks like you could do something like
{#if count > 0}
<button on:click={decrement}>
...
</button>
{/if}
{#if count > 0}
<button on:click={decrement}>
...
</button>
{/if}
CDL
CDLOP6mo ago
I’ll check this out when home!
vince
vince6mo ago
Idk if that'll work I was just going off of your syntax 😂
b1mind
b1mind6mo ago
yea should work
CDL
CDLOP6mo ago
Yep tested on the svelte tut and it works well. Thanks peeps. Now to understand form actions so I can build this app 😤
Want results from more Discord servers?
Add your server