4 ms·
FWIW you can of course implement createSignal in four lines of code, if you prefer the ergonomics of that: function createSignal(initial) { let value = $stat
by rich_harris 3y ago
FWIW you can of course implement createSignal in four lines of code, if you prefer the ergonomics of that:
function createSignal(initial) {
let value = $state(initial);
return [() => value, (v) => value = $state(v)];
}
Note that the Solid change is _not_ 'one step' — the `completed` property is being turned from a property to a function, which means you must update all the usage sites as well. Using getters and setters also allows you to use Svelte's convenient `bind:value` approach, which is much less verbose than using the equivalent event handler code. And don't get me started on the [type narrowing issues](https://www.typescriptlang.org/play?#code/C4TwDgpgBA4hzAgJwDwBUB8UC8UAUAlDlmgNwBQokUAyvIqpjvgG4BcUaR2JF5AZgFcAdgGNgASwD2wqKKQQAhohoSA5sMUAbdBjwttgiBy4cA2nATJdAGlr1rmALpQA3uSieoC4IKSyzQmIoAy0jO30Tbix9Q2hcFgInCgBfcnIAegyoAFo89IlhBn5FUWgAVQBnZDcPL00AW2MoSuAkQrVU9NEZVqgzQWqkO2rgKuQXXHklFXVNHXGkKAAfKGFBLS09dy81xSaOAHJ20QALQ-IUgj4JfnxB5EIiHa8sqFOJABMOkLjKqAARhAPsJPlAhGJJDI5NotP8kFJBJJhBAtCA6p43gpKhtgP9ClBFMJhFIQD8qNBkAikJUMXJelItBAAHRaKRqPAAA1OqLZUAAJK4HkhCMzGhAUgBCTnXS5AA https://www.typescriptlang.org/play?#code/C4TwDgpgBA4hzAgJwD...).
There's nothing _wrong_ with the Solid approach, but the ergonomics aren't to our liking. If we need to take a hit in terms of verbosity, we'd rather do it once at the declaration site than n times at every usage site.
- onsclom 3y agoThese are great points! Thanks for the super thoughtful reply. I'm actually sold. 1. It's really nice that it's so easy to make a `createSignal` 2. I didn't realize that in Solid you had to update all the usage sites too, so now I'd rather stick to the Svelte usage. Nested reactivity is not as effortless as I thought in Solid. get done() { return done }, set done(value) { done = value }, get text() { return text }, set text(value) { text = value } Looks a bit boilerplate-y maybe, but keeping your preoptimized call sites is totally worth it. This might be common enough that some syntax sugar might be worth it? The short hand for: todos = [...todos, { get done() { return done }, set done(value) { done = value }, get text() { return text }, set text(value) { text = value } }]; Could be: todos = [...todos, { $done, $set }];