News for hackers. Stuff that matters.
Re:Solid.js feels like what I always wanted React to be by ryansolid on Tuesday March 01, 2022 @02:06AM (#30510644)
What does a reusable (ie import from another file) `useAutoCounter` look like in Svelte?
Re:Solid.js feels like what I always wanted React to be by alskdjflaskjdhf on Tuesday March 01, 2022 @03:25AM (#30511199)
Probably the equivalent would be a custom store, something like this:

  <script>
    import { writable } from "svelte/store";
  
    function autoCounter(interval, initialValue = 0) {
      let { subscribe, update } = writable(initialValue);
      setInterval(() => update((n) => n + 1), interval);
      return { subscribe }
    }
  
    let counter = autoCounter(1000);
  </script>
  
  <div>The count is: {$counter}</div>
Re:Solid.js feels like what I always wanted React to be by ryansolid on Tuesday March 01, 2022 @04:02AM (#30511513)
Yep. Looks sort of like the Solid example in the article. It's basically my auto-response to Svelte syntax. Once you do anything in Svelte it is more or less the same thing.

Just different priorities. In Solid you can take that code as is in the component and hoist it out to a function above and presto.. store. It's all the same thing everywhere. Same patterns, same usage, same building blocks. No new APIs, no new syntax.

It is nice when first learning not to worry about Svelte Stores and use the convenient syntax. It is also nice to learn something once and use it everywhere.