Ragnar learns svelte
opening a thread here just so I can ramble without bothering people in #chit-chat or #off-topic
114 Replies
https://svelte.dev/docs/svelte/compiler-errors#constant_binding
just got the most unhelpful error I've seen in a while...

change const to let
wait
nevermind
well it did fix it... but... I don't like the implications of it...
i know what you mean
there's no writable computed in svelte ?!?!
please someone tell me there's a way to do it declaratively... this irks me
@b1mind sorry to ping, but I need to know and I'm too lazy to search for myself xD
damn it I scrolled down in the doc... https://svelte.dev/docs/svelte/@const
why would anyone make this a thing ? 😭
webstorm integration is not up-to-date or am I doing something wrong ? 'cause it's working :/

"it's just JS guys"
you lied to me B1... how dare you!

ah... I created an infinite loop of errors...
woups
how?
I tried to display the todos in 2 lists, one filtered on the done and one on the todo
and... it exploded
I guess it's because of the $effect I used here https://discord.com/channels/436251713830125568/1429163039453745213/1429166444301647872
but so far I got no clue
and if it's that... I don't know how to fix the issue
ok so now I'm stuck...
how do i do the following Vue thing in svelte ?
basically, convert the boolean "is checked ?" to the less boolean "if it is checked when was it checked ?"
i.e. writatable computed
I don't like this...
I have a let that I should not set because if I do the template complains and an update function that is decoralated from the derived it will indirectly update
someone please tell me I'm doing things horribly wrong
pleeeease
Imma start throwin hands...
https://svelte.dev/docs/kit/$lib
import aliases are an anti pattern
just like logic in the template
I’m excited to see you try react if this is how you are with svelte 🤣
you want me to murder someone ?
that's no nice...
forthe someone
imma try to add the server bits tomorow, but no promises
I think this would be easier if you just had a done property on the state and used that property directly for bind
Or pass the value without bind if using derived and use event handler
Oh todo isn't a state it's prop hmmm. Yeah I'm not deep enough in svelte to answer this
I want it to be a date so I can sort them by that date
I meant keep the doneOn and add an extra done property just for the checkbox input
Derived value just doesn't make sense to be a bind property to me since that implies you're directly changing it instead of it reacting to changes in state
From what I understand of how the doc explains it you are indeed not supposed to do it like this. But i don't like having intermediary variables, I feel like it just adds noise
I guess the correct way would be to have a
$state<boolean>, bind that on the checkbox and have an $effect do the conversion into date when that boolean switches.
But that means doing the comparison with null in 2 places, one in the effect and one to initialize the state
And... I don't like it one bit
Didn't get the motivation to work on it today, I'll try again tomorrow after coming back from workCan you just not use
bind:value and simply use value?todoList • Playground • Svelte
Web development for the rest of us
not sure if svelte playground is shareable but i tried something. The initial code is taken from one of the svelte tutorials https://svelte.dev/tutorial/svelte/deferred-transitions here
don't think it's that good since it's passing state through 2 child (should probably use stores or something but I never got that far) and I am not sure if bind function synatax is mean to be used like this
I just noticed that this is mutating state without bindable. Wonder why this doesn't generate warning

because you modify the value of a property of the object passed as a prop, not the object itself, so you don't break the reference link of the prop, so the reactivity system can still do it's thing
that's... not what I had in mind...
That feels like it's circumventing the reason bindable exists for
honestly I don't understand why bindable exist...
Also svelte will throw a warning if you directly bind like this
bind:checked= {todos[0].done}
It's only the each template that for some reason ignores itbind warning • Playground • Svelte
Web development for the rest of us
got any idea why it's doing what it's doing in the vid BTW ?
'cause I'm at a loss
I suppose that's because of how I initialise the $state, but if that's not how you should do I don't know the correct way
Are you using a key in your each directive
I assume you're using each somewhere
hu... IDK, here the bit of the template that does the each
Index is different from key i think
Index is just for your code use while key helps svelte track objects
yeah index is just how far in the iteration we are
so, I did have a key, I added one and now it's even more broken
Lol
Don't think I can speculate more from this. Maybe throw the whole code in svelte playground or something else and I'll mess around tomorrow.
Too late now
I used the kit template so I have no idea if it'll work in the playground
i'll try tho
👍
https://svelte.dev/playground/b4d09314464d4fd5bee9eaac42346b1f?version=5.41.1
don't click anything without having a breakpoint for exceptions or your tab will crash
fucked up todo list • Playground • Svelte
Web development for the rest of us
it's really not happy but if I try to do what the error message tels me to it gets worse...
how in the fuck does this :
compile to this ?
so... from 5.9.0 onward you can do this :
which is more or less what I was looking for with a writable derived
but I don't like the fact that the getter and the setter are independent thingies
they don't make sense individually so why can't I have them as a single thing ?!
so... now that what I had before works without complaining or crashing my tab
how the fuck do I get logs from the
+server.ts files?
and how do I debug them too...
'cause the doc is not talking about that one bit, even thought it's like... kinda important
nevermind... it's still complaining
either I do this and it complain's about it not being reactive :
or I do this and the LSP complains about binding in each blocks something something...
(╯°□°)╯︵ ┻━┻
so how the fuck am I supposed to do it then ? there's no way to send an update event like in vue, it doesn't like when i bind or when I don't bind
what does it want me to dooooooo
so... this works...
but I don't like it
why do I need to have so much logic in the damn template to do anything ?!
ok ... I got side tracked by zsh not killing the server when I kill a shell... and I don't know how to fix it either
imma stop here before I break somethingfucked up todo list • Playground • Svelte
Web development for the rest of us
I did a similar thing
but it is complaining that I am binding a non reactive property

dervied using deeply reactive proxy should be reactive tho
I have no idea what is its problem
this doesn't get any warnings
$derived variables aren't deeply reactive, meaning that if you reassign them (instead of mutating) the system won't track that & the connection will be broken.should I not mutate the derived values then?
You can. My understanding is that the main issue is with reassignment.
hmm so basically if instead of mutating todo like this
todo.doneOn = new Date()
if i reassign the whole todo todo = new Todo()
it won't reflect any changes in the parent state
right?I'm pretty sure that's correct.
so that is why svelte is fine with me binding properties because it knows reassigning them will have changes
but reassigning the object from derived array won't
(Also, as a note, it would traditionally be
doneAt.)I just copied from ragnar's code
but my english is bad so i also didn't notice 😆
doneOn makes some sense, but I've seen bunches of timestamp fields in database designs & they're uniformly <event>_at.gotcha
I get confused since you use on with time but with days. "I am free on friday" while specfic time uses at? "I will be free at 12 pm" tho this is an awkward example
That's the general rule I use. I'd say: “I was born on February 2ⁿᵈ at 11:37.”
I initially wanted to just record the date, so on was ok... And then I used a timestamp instead xD
ok... I'm lost, is this not how you use
bind:this ? why is form null when onChange is called ?fucked up todo list • Playground • Svelte
Web development for the rest of us
ah... found out why...
the onchange is called after the form is dismounted and disposed of
Can I ask why you're using @attach instead of just passing a submit handler to the form
where?
ah in the app
it's because I wanted to prevent the submit event
and you can't do that in a onsubmit because it's set as passive
but now that I'm trying to have a backend DB I will need to actually submit the form
The attach function runs in an effect iirc
And you're not doing any cleanup
So that might fuck something up
I don't need any clean up
I'm not dirtying anything and the element is never unmounted
Won't it attach multiple event listeners if any values used in that function change
Unless I'm misremembering
it only reruns it when the element it's bound to changes
from my understanding
so if I unmounted the component or had a if block arround the form element
Hmm I'll need to reread the docs
Also I don't understand this. What do you mean by passive
Ahh didn't know about this. Thanks
that's... new

eh?
how did you do that!?
IDK
204 - no content
500 - internal error
yup
alright... I got it to work
my experience so far is... meh
is there no way with
use:enhance to keep the focus on the field that was focused before the submission but do all the rest of the logic it does ?
I tried passing this as a parameter of the action, but to no avail
the focusing of body is still done after my call to focusif you're storing the date, why not store it as a timestamp, which is timezone independent?
you're storing way more than what you need to
because I'm just json stringifying the object as is
you can use a replacer function
I'm not doing ANY serialization work whatsoever xD
oh
it's automatic?
hu... no
this is my save function :
I'm just too lazy to add a serde layer XD
I don't really want to go and make a shitty ORM
(and yes there's more types than code in this function)
I might need to make a serde layer tho... 'cause dates in json are just strings...
which means my sorting is all broken
why would it be broken?
just sort alphabetically
because I'm calling
.valueOf on the dates
and that fucks it upalright here's the repo, feel free to peruse and judge
https://github.com/Ragnar-Oock/sveltekit-todos
GitHub
GitHub - Ragnar-Oock/sveltekit-todos: just a small todo list thingy...
just a small todo list thingy to try out svelte kit - Ragnar-Oock/sveltekit-todos
I'll need to make an adapter for xoram, but I have no idea how that would work...
and I'm too tired to try to figure it out
let stupidForm: HTMLFormElement; 🤣yes xD
I don't like the idea of having a bagilion forms on the page:/
🤣 how do you know that that isn't the smart one?
because i made sure it's dumb
so, it should be
dumbForm, not stupid 🤔same diff
dumb is dumb, stupid is stupid 🤔
quick maths
I often keep a form submission from going to the server with
evt.preventDefault().from reading the code, don't think he has access to the raw event
but i may be wrong
you have, but id doesn't do anything because the listener is set as passive
meh :/
ISO GMT timestamps, YYYY/MM/ddTHH:mm:ssZ, will sort chronologically lexigraphically.
and alphabetically
yes, but that mean I need to do serde
(or something else is broken but I don't care right now XD
I'll check another day
but
2025-10-21T22:10:10Z will always come after 2025-10-21T10:20:30Z
sorted ascendingly
however you sort it, it will always be correct
so, something else is wrongyeah, but that doesn't work if one is a string and the other a date
it does
a.toString() > b.toString()i'm not using toString at the moment
https://discord.com/channels/436251713830125568/1429163039453745213/1430299259655163965
valueOf works too
yeah, forgot that it gives an unusable format
wait, no, it doesn'tif it did I would not have issues...
Date.parse worksDate.valueOf gives a number while String.valueOf gives a string
you can pass a date to it
yes... but that means I need to do serde, and I was too lazy for that
why serde for
Date.parse?
it's a built-in method
and dates are comparable in jsbecause I can't
Date.parse my label or Id can I ?
so I need to do each property individually:think:
hense... serialization, deserialization
serde
Well, you can to a conditional on
instanceof Date & call toISOString().I know how to do it XD
I just didn't want to do it
Hum... I want to play with service workers again... Might try to have one that proxies calls and send them as batches instead of one by one when multiple todo's are updated