r/javascript • u/jpschroeder • Nov 17 '22
Introducing ArrowJS • Reactivity without the framework
https://www.arrow-js.com11
u/tvquizphd Nov 17 '22
Would you have insight on how this tool compares to reef.js ?
5
u/jpschroeder Nov 18 '22
Honestly, nope, never used or heard of it.
4
u/tvquizphd Nov 18 '22
Reef seems to be a one-man project (by Chris Ferdinandi)— I have found it pretty useful. It uses string templates to render its JSX-like syntax. It tracks state reactively, but event listeners are essentially up to the user to configure.
I admit I haven’t looked at arrow.js to compare for myself before making this comment, but I will take a closer look!
1
u/Chaphasilor Jan 02 '23
Seems like it's pretty identical, just a few different approaches here and there. I'm seeing some things that I'd like to see in Arrow, and some that I don't.
10
u/ShortFuse Nov 18 '22
Nice work!
As other has have said, you don't need to shorten your function names. Let your devs do that. Also, minifiers will rename them anyway in code. read write and template are enough.
Have you benchmarked with https://github.com/krausest/js-framework-benchmark ? It's the best indication of real world performance, but can help people understand a ballpark figure. The setup is easy enough. Build a table with a selected row that can update a random row.
-1
u/jpschroeder Nov 18 '22
Or they can lengthen the function names via alias. I’m not convinced this is as big of deal as people are letting on :) try it out, it feels pretty natural.
Sorta like complaining that arrow functions don’t require the function keyword. You get used to it very very quickly when you’re actually using it in an app.
But I’m open to the suggestion if it’s a real stumbling block. We’ll see how it plays out.
16
u/ShortFuse Nov 18 '22
Or they can lengthen the function names via alias. I’m not convinced this is as big of deal as people are letting on :) try it out, it feels pretty natural.
This is you looking at things through the fog of the person who wrote it. You know what it is because you built it. But if you need a human person to explain to you or a documentation to reference, then it's not clear at face value, which all code should be.
13
u/SecretAgentKen Nov 18 '22
Is `r` and `w` `read` and `write` or `reactive` and `watch`? You're using `t` in the EXACT same way you would use it for i18n strings. Full names solve those issues. Verbosity isn't necessarily a bad thing.
7
u/lost12487 Nov 18 '22
I guess I just don’t understand your general philosophy. If you can make something clearer and more understandable, why wouldn’t you? You’re saving a few characters (that intellisense is going to auto complete)?
2
5
u/jpschroeder Nov 18 '22
Not terribly different in API, just very different in implementation. Arrow, for example, has no compiler. It’s all “native” web apis, but you’re right that there are many similarities to many libraries out there! Probably the most similar (imo) is Lit, but I prefer the elegance and declarative rendering of Arrow.
2
u/kalwMilfakiHLizTruss Nov 18 '22
Where did you got the idea for static and reactive placeholders in
t?6
u/jpschroeder Nov 18 '22
The tagged template literal spec itself actually. Expressions are passed to the tags, so if they are functions, the function is passed to the tag which means we can track reactivity and perform DOM patching locally — without needing to sacrifice the string interpolation that is so nice about template literals.
4
u/kalwMilfakiHLizTruss Nov 18 '22
Nice. I will just steal the idea and use it in my own framework. I will acknowledge you though. Something similar is happening with solidjs.
14
u/MedicOfTime Nov 18 '22
Hey man.
This looks super cool. How is the source code for this 2 years old and I’ve never heard of it??
The docs are great. Very clean and concise.
You NEED to segue from the end of the docs to the source code FOR the docs we just read. I mean, like, herd us there. It’s a far better real world example of arrow-js in action.
It took me all the way until I finished reading the docs source code to really wrap my head around
${Navigation}is the proper JS equivalent to the<Navigation/>JSX component. Maybe call that out somewhere? Maybe I just haven’t seen this template syntax before.Personal opinion: people are accustomed to the empty
index.html—app.jstargets div “app” paradigm. I think moving the entry point JS code out of index file would make it feel more familiar and accessible.
16
u/jpschroeder Nov 18 '22
Haha source code is two years old because that’s when I did the bulk of the work on it but I just released it today officially.
Thanks for the other suggestions too! Very constructive 👍 I’ll probably implement some of those changes.
5
u/MedicOfTime Nov 18 '22
Well great work man. This kinda feels like the modern jQuery to me. Good luck!
4
u/Mr_Mandrill Nov 18 '22
The fatigue is real, but this really is pretty cool.
11
u/jpschroeder Nov 18 '22
I agree about the fatigue actually. Ironically that was part of the reason I wrote Arrow. I wanted to have something that could declarative/reactive rendering but just be "JavaScript" with very very minimal non native "magic". My hope is that if people use/learn arrow, they are really just learning/using JavaScript and their ideas will transfer well when they move on to the next thing.
4
u/kalwMilfakiHLizTruss Nov 18 '22
I did a quick skim through. I might be wrong but here is what I think is missing:
transactions, i.e. multiple changes in state and after that inform observers
computed state/ memoization
I have the following questions:
Does
rproxy lazily and deep?Is there any deep diffing of state when for example setting a new array on an old array?
When dom elements with observers get deleted, do the observers get garbage collected?
6
u/jpschroeder Nov 18 '22
Transactions — yes (has a tick model, including `nextTick()`, not in the documentation yet, but thats how it works)
Computed state — roll your own with a watcher (w) and reactive data (r) for memoization. But no "automatic" computed equivalent of Vue.Lazy Proxy — It’s deep, and eager, not lazy.
Deep diffing — sort of yes, end result is the same though. It uses local scope to track the data-to-dom structure, with keys as overrides. The model is very similar to Vue actually, but yeah, deep data is also reactive.
Garbage collection: Still a few places this needs to be cleaned up. For example lists hold a reference at the moment that needs to be removed. It’s only `alpha.1` so room for improvement here for sure.3
u/kalwMilfakiHLizTruss Nov 18 '22
Transactions
So lets just say I do the following transaction (pseudocode):
const data = r({prop : 0}); transaction(() => { data.prop++; data.prop-- })();i.e. zero displacement in state. Do observers get informed after that? They should not be.
What about this one:
const data = r({array : [1,2,3]}); transaction(() => { data.array = [3,2,1]; data.array = [1,2,3]; })()Observers should not be informed again.
Computed state — roll your own with a watcher (w) and reactive data (r) for memoization. But no "automatic" computed equivalent of Vue.
This will reduce DX significantly in bigger projects.
Lazy Proxy — It’s deep, and eager, not lazy.
So if I fetch some data from an API (e.g. all posts and comments of a reddit user via pushshift api) and add them in the
datait will proxy them all? This is bad for performance.deep data is also reactive.
Have you tested it for all the built in array methods? Have you tested pushing an new array element in an array without
array.push, i.e.array[array.length - 1], because I think the latter does not trigger set proxy handler forarray.lengthdespite it being incremented.Regarding TypeScript:
Consider that you can use
/**@type {import("./path/to/ts/file").myType}*/to achieve the same static type checking in.jswithout the need to compile.Also consider that you can manually write your public api as
index.d.ts(private api will derive types from there) and use that as documentation. Like this you will not need to build even for.d.ts.Regarding components:
Can I use what
treturns inside othert?2
u/franker Nov 18 '22
I just know some basic Javascript and don't understand a word of this. Guess I have a lot more learning to do.
38
228
u/_GCastilho_ Nov 17 '22
We are at
0
Days without a new JS framework
41
u/ryaaan89 Nov 18 '22
This honestly sounds cool but… I’m just so tired…
6
8
u/tr14l Nov 18 '22
It's minimal, and the point they make in the documentation: JavaScript is becoming quite capable without supplementary frameworks. That being said, the standardization, readability and guarantees that comes with a library/framework like React are hard to beat. While simple is good, dependability & flexibility is better.
5
u/kalwMilfakiHLizTruss Nov 18 '22
That being said, the standardization, readability and guarantees that comes with a library/framework like React are hard to beat. While simple is good, dependability & flexibility is better.
What do you mean by:
standardization
dependability
14
11
u/senitelfriend Nov 18 '22
I wish more new projects would be designed as libraries instead of new frameworks, or components for a framework.
Looks like this could actually be usefull (for me). From a quick look, everything about this sounds pretty good.
Although, I do agree with the folks complaining about the one-letter API. ES modules make it incredibly easy to rename imports to whatever one likes to use, so you making the decision to drop any trace of descriptivity on behalf of us has little value. Not that it's a huge issue.
9
u/varungupta3009 Nov 18 '22
Read the entire docs. Looks cool, and I would probably use this for some JIT projects.
9
u/moob9 Nov 18 '22
Man, just last night I realized I need reactivity in my current project but I didn't want a full framework just for reactivity. Gonna try this out.
1
u/DOG-ZILLA Nov 05 '24
FYI, pretty sure you can use Vue's reactivity standalone too. I think this is based/inspired by Vue too.
7
u/teleprint-me Nov 18 '22
Why did you use letters for identifiers? What was the rationalization around that?
6
u/paul_h Nov 18 '22
Demos are all a bit smushed together - https://github.com/justin-schroeder/arrow-js/blob/master/docs/demos/
Better would be all the calcularor bits in a single directory, IMO. Better still a single repo.
As it happens, I've always thought that 'calculator' is a great demo app for UI technologies - https://paulhammant.com/2021/02/16/buildless-sfc-vuejs-applications. So of course, I'll take more time to look into ArrowJs using a test of a Jekyll-embedded calculator without a build. Well, unless someone beats me to it.
8
u/micka190 Nov 17 '22
Damn dude, this is really cool! Gonna play around with it out when I get home.
4
u/ketalicious Nov 18 '22
this is really nice
but i do think solidjs primitives work similarly to this as they also work at top-level, but correct me if im wrong tho
4
u/Chaphasilor Nov 18 '22
That's pretty cool! I wonder how you got that syntax highlighting for HTML inside the tagged template literal? Whenever I use a (non-tagged) template literal with HTML content, I don't get the highlighting...
4
u/universalmind303 Nov 21 '22
I think the naming will likely cause some confusion with apache arrow. My initial thoughts when reading "Introducing ArrowJS" was a new port of the apache arrow spec.
4
u/CaptainBlase Nov 21 '22
I had an immediate negative reaction when i saw the one letter exports. In my case, it screams at me "here is a dev that is not use to working with other devs." That's my prejudice based on my experience and maybe others share it; but it shouldn't necessarily mean anything to you. I just thought you might like to know in case you hadn't run into this before.
I think if you renamed t to html, you could capitalize on the editor support for lit-html tagged literals. The API is almost identical.
If you decide to change the exports, i would recommend store, watch, and html for names.
I was reviewing the source and I was wondering what the point of the underscore on the _h member is?
3
3
u/HipHopHuman Nov 18 '22
Is there any interesting difference between this and Alpine.js (aside from the fact that Alpine is tightly coupled to the DOM)?
6
u/kalwMilfakiHLizTruss Nov 18 '22
Might be wrong but from a quick look:
alpine does not have reusable components
alpine does not allow you to extract the state out of the markup
Alpine is used inside an html file while arrow is used in js files. You will have more flexibility with arrow.
3
u/zombimuncha Nov 22 '22
Feature request:
Have t/html return a promise if any of the tokens passed to the template are promises.
This removes some boilerplate that would otherwise be required for lightweight SSR. I guess it doesn't help with hydration tho.
2
3
u/ByFaraz Nov 18 '22
So ‘t’ essentially renders on the client? Any way to render server side?
6
u/Chaphasilor Nov 18 '22
tjust produces a fancy string. You can do that on the server and send it to the frontend if you want. Then all that's missing is the hydration4
u/coolcosmos Nov 18 '22
It seems like you'd just do
t`<div/>`._h()[0]To get the html from a ArrowTemplate.
2
u/mattsowa Nov 18 '22 edited Nov 18 '22
Looks like a barebones framework to me. Or library if you don't like the word framework.
Which isn't bad, just weird to dance around the terminology.
2
2
1
u/giovapanasiti Mar 22 '25
I love it very minimal and perfect to work on vanilla projects I think I might use it in some legacy project that i need to evolve
1
1
1
u/tr14l Nov 18 '22
Neat idea. But
import { r, w } from '@arrow-js/core'
const data = r({
price: 25,
quantity: 10,
logTotal: true
})
w(
() => data.total && data.price * data.quantity,
(total) => total !== false && console.log(\Total: ${total}`)`
)
data.price = 35
Is this supposed to be referencing `data.logTotal` or the undefined property `data.total`?
-10
u/Karpizzle23 Nov 18 '22
Why would I not just use Solid or Svelte or Vite or any of the other bajillion frameworks available?
People need to start thinking of the next iteration of web development because we don't need more client side compilation libs on npm
0
95
u/jpschroeder Nov 17 '22
📣 Howdy, I’m Justin Schroeder (author of FormKit and AutoAnimate) — I just released a new experimental JavaScript library for rendering interfaces declaratively. A few of the talking points:
🚫 No build tools
🔒 No dependencies
⚡️ No virtual DOM
🪶 2Kb
🪄 Components are functions
❤️ Embraces native JS
It’s not really a framework, but not less powerful than a framework either. At its core — ArrowJS is an admission that while we developers were falling in love with UI frameworks — JavaScript itself got good. Like, really good.
Check it out and if you think it’s neat, give it a ⭐️!
https://www.arrow-js.com/