News for hackers. Stuff that matters.
Re:VanJS: A 0.9KB JavaScript UI framework by ping-monkey on Wednesday December 20, 2023 @11:06AM (#38710153)
Try to replicate a sample app made by VanJS with Vanilla JS and check the difference
Re:VanJS: A 0.9KB JavaScript UI framework by qayxc on Wednesday December 20, 2023 @11:23AM (#38710351)
challenge accepted (app #2, since #1 is just static), BEHOLD:

  <script>
  const Counter = () => {
    let counter = 0;
    const updCounter = (delta) => {
      counter += delta;
      document.getElementById('count').innerText = `♥ ${counter}`;
    };
    document.getElementById('up').addEventListener('click', () => updCounter(1));
    document.getElementById('down').addEventListener('click', () => updCounter(-1));
    updCounter(0);
  };
  Counter();
  </script>
  <span id="count"></span>
  <button id="up">*thumbs-up*</button>
  <button id="down">*thumbs-down*</button>
15 LoC vs 7 LoC, zero dependencies vs. 0.9kB gzipped or 2.7kB minified. Additional learning curve: 0h vs 1h (allegedly).

Honestly, I am biased and think Vanilla JS wins :D

Re:VanJS: A 0.9KB JavaScript UI framework by flying-pig on Wednesday December 20, 2023 @12:49PM (#38711419)
The fact that you have to manually maintain the binding between states and UI elements and propagate state changes to UI elements is exactly the thing offered by VanJS, or other popular reactive frameworks (despite with a much larger bundle size)
Re:VanJS: A 0.9KB JavaScript UI framework by qayxc on Wednesday December 20, 2023 @01:40PM (#38712063)
Sure thing. But it's also one of those things that comes back to bite you when you least expect it: https://vanjs.org/advanced#why-not-dom-valued-states
Re:VanJS: A 0.9KB JavaScript UI framework by flying-pig on Wednesday December 20, 2023 @04:31PM (#38713904)
I don't get you. Could you elaborate?
Re:VanJS: A 0.9KB JavaScript UI framework by qayxc on Thursday December 21, 2023 @05:52PM (#38728234)
In the example I linked, the VanJS state cannot contain an element reference.

This would lead to unexpected results. So you have to be careful with state management, regardless of whether it's automated or not. There are similar pitfalls, including performance issues, with any state management system, hence dedicated state management solutions like Redux exist to address this.

The core of the argument is that complexity sometimes cannot be avoided. You can quickly wind up with moving this complexity elsewhere, e.g. by pulling in an additional library dependency. This results in having to learn, master and manage additional dependencies. Whether that's fundamentally better than the explicit and straight forward way depends on the scope of the project and its specific requirements. There's no silver bullet in any case and pros and cons with every approach.

Re:VanJS: A 0.9KB JavaScript UI framework by flying-pig on Friday December 22, 2023 @07:54PM (#38740605)
Tbh I can't follow your logic here. You mentioned that in VanJS there are things that need to be careful with. But the same is true for other frameworks, even for plain Vanilla JavaScript. Thus what exactly the point that you're trying to make?

Complexity can't be avoided for extremely complex use cases. But that doesn't mean a simple solution that can work for most of the use cases has no value.

Re:VanJS: A 0.9KB JavaScript UI framework by fs_tab on Wednesday December 20, 2023 @02:01PM (#38712280)
Elements with an id are globals, so you do this (not that it's recommended):

    count.innerText = 
    up.onclick = 
    down.onclick =
etc
Re:VanJS: A 0.9KB JavaScript UI framework by qayxc on Wednesday December 20, 2023 @02:24PM (#38712518)
Technically, every element that's not in the shadow DOM is a global. But yes, there's plenty of ways to implement this and an argument to had for most of them.
Re:VanJS: A 0.9KB JavaScript UI framework by fs_tab on Wednesday December 20, 2023 @03:01PM (#38712922)
Right, I should have clarified that they're properties of the window object.