TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

VanillaJSX.com

538 点作者 novocantico9 个月前

38 条评论

spankalee9 个月前
Returning actual DOM nodes entirely blunts the big advantage of JSX (and non-JSX libraries like Lit) - which is their immediate mode style API, and UI=f(state) model.<p>You want to return a description of the DOM, rather than the real DOM, because you want to be able to reevaluate your templates repeatedly with new state, and efficiently update the DOM where that template is rendered to.<p>All the examples here use imperative DOM APIs to do updates, like with this:<p><pre><code> function TodoInput(attrs: { add: (v: string) =&gt; void }) { const input = &lt;input &#x2F;&gt; as HTMLInputElement; input.placeholder = &#x27;Add todo item...&#x27;; input.onkeydown = (e) =&gt; { if (e.key === &#x27;Enter&#x27;) { attrs.add(input.value); input.value = &#x27;&#x27;; } }; return input; } class TodoList { ul = &lt;ul class=&#x27;todolist&#x27; &#x2F;&gt; as HTMLUListElement; add(v: string) { const item = &lt;li&gt;{v}&lt;&#x2F;li&gt; as HTMLLIElement; item.onclick = () =&gt; item.remove(); this.ul.append(item); } } </code></pre> Avoiding those `input.onkeydown = ...` and `this.ul.append(item)` cases, and instead just iterating over items in your template, is probably the main benefit of a VDOM.<p>(The problem with VDOMs is that diffing is slow, a problem solved by using templates that separate static from dynamic parts, like Lit - a library I work on).
评论 #41272196 未加载
评论 #41271285 未加载
评论 #41271169 未加载
评论 #41271681 未加载
评论 #41271408 未加载
评论 #41275264 未加载
评论 #41276060 未加载
评论 #41272889 未加载
评论 #41274424 未加载
评论 #41271576 未加载
评论 #41271946 未加载
评论 #41278545 未加载
评论 #41275162 未加载
评论 #41271334 未加载
评论 #41273594 未加载
novocantico9 个月前
Thanks for taking some interest in my project. It came from being frustrated with the state of SSGs over the past 10 years. I mostly just make static websites, and I wanted something that was simple and intuitive to me, and JSX seemed like a great fit. But I got very tired of the disproportionately scaled complexity of JSX frameworks like React. Long story short, I made an SSG that just renders JSX as strings. It was natural to extend that to the browser to just render JSX as DOM elements. And in a few cases (mostly layout) it lends well to shared components. Overall I&#x27;m happy with what I came up with, although some of it is admittedly a little hacky, and IDE support isn&#x27;t as good as it could be.<p>[edit] Oh also, this solution works really well for SEO. That&#x27;s another problem I didn&#x27;t find solved well in other JSX frameworks.
评论 #41271744 未加载
评论 #41274050 未加载
评论 #41271626 未加载
评论 #41271291 未加载
评论 #41273477 未加载
评论 #41272184 未加载
评论 #41271421 未加载
cribbles9 个月前
These &quot;what ifs&quot; are kinda funny because the origins of JSX can be traced back to Facebook&#x27;s XHP[1], which took explicit inspiration from E4X[2], an early JS standard that looked and behaved similar to the library described here.<p>[1] <a href="https:&#x2F;&#x2F;engineering.fb.com&#x2F;2010&#x2F;02&#x2F;09&#x2F;developer-tools&#x2F;xhp-a-new-way-to-write-php&#x2F;" rel="nofollow">https:&#x2F;&#x2F;engineering.fb.com&#x2F;2010&#x2F;02&#x2F;09&#x2F;developer-tools&#x2F;xhp-a-...</a><p>[2] <a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;ECMAScript_for_XML" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;ECMAScript_for_XML</a>
评论 #41271249 未加载
评论 #41271968 未加载
recursive9 个月前
I also made a UI library based on the idea of jsx template expressions that produce real DOM nodes. It also binds model objects to attributes, eliminating some of the imperative event handler boiler-plate. I think it&#x27;s a great idea, but of course I would.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;tomtheisen&#x2F;mutraction">https:&#x2F;&#x2F;github.com&#x2F;tomtheisen&#x2F;mutraction</a><p>It lets you do stuff like this.<p><pre><code> const model = track({ clicks: 0}); const app = ( &lt;button onclick={() =&gt; ++model.clicks }&gt; { model.clicks } clicks &lt;&#x2F;button&gt; ); document.body.append(app);</code></pre>
merlindru9 个月前
VanJS deserves a mention here! <a href="https:&#x2F;&#x2F;vanjs.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;vanjs.org&#x2F;</a><p>Another interesting thing is that other JSX libraries like Solid.JS also return DOM nodes, and I love that this idea is gaining traction<p>The closer we get to the platform we&#x27;re using, the better. Being removed by layers of abstractions CAN be useful, but in practice, I haven&#x27;t found a use for abstracting away the platform. (yet.)<p>Maybe huge projects like Facebook benefit from this tho (which I haven&#x27;t worked on)
评论 #41274460 未加载
sophiebits9 个月前
These examples are cool but I think it’s important to note that none of them show components whose props can change over time, since that ability doesn’t seem to be modeled at all. Clever if you don’t need that but I’m having trouble seeing how it would scale to more complex apps.
评论 #41271585 未加载
评论 #41272290 未加载
评论 #41271162 未加载
评论 #41271315 未加载
flowerlad9 个月前
This is very similar to Vanilla TSX: <a href="https:&#x2F;&#x2F;github.com&#x2F;wisercoder&#x2F;uibuilder">https:&#x2F;&#x2F;github.com&#x2F;wisercoder&#x2F;uibuilder</a><p>Here’s an app written using Vanilla TSX: <a href="https:&#x2F;&#x2F;github.com&#x2F;wisercoder&#x2F;eureka&#x2F;tree&#x2F;master&#x2F;webapp&#x2F;ClientApp">https:&#x2F;&#x2F;github.com&#x2F;wisercoder&#x2F;eureka&#x2F;tree&#x2F;master&#x2F;webapp&#x2F;Clie...</a>
config_yml9 个月前
Reminds me of Action Script 3 which had XML at the core of the language. It was a fun language to work with, but famously failed to become ES4. Oh well, took us 10+ years to arrive close to that with Typescript and JSX.
评论 #41270971 未加载
评论 #41271023 未加载
girvo9 个月前
Does the final example not work in Firefox for anyone else? It worked in Edge, but not Firefox for me<p><pre><code> Uncaught (in promise) TypeError: Map.groupBy(...).entries().map is not a function</code></pre>
评论 #41272044 未加载
slmjkdbtl9 个月前
I never understand the appeal of JSX over something like<p><pre><code> h(&quot;div&quot;, {}, [ h(&quot;p&quot;, {}, &quot;this is easy&quot;), ...list.map((l) =&gt; h(&quot;li&quot;, {}, l), ]) </code></pre> With this you automatically get loops, variable interpolation etc without having to invent a compiler and new syntax. Can someone help me understand?
评论 #41271664 未加载
评论 #41274181 未加载
评论 #41271643 未加载
评论 #41271684 未加载
评论 #41276771 未加载
评论 #41271654 未加载
评论 #41272074 未加载
评论 #41275066 未加载
ibash9 个月前
People forget what problem the virtual dom and react is supposed to solve.<p>No better article than this: <a href="https:&#x2F;&#x2F;blog.vjeux.com&#x2F;2013&#x2F;javascript&#x2F;react-performance.html" rel="nofollow">https:&#x2F;&#x2F;blog.vjeux.com&#x2F;2013&#x2F;javascript&#x2F;react-performance.htm...</a>
评论 #41271072 未加载
评论 #41271963 未加载
评论 #41271136 未加载
评论 #41271645 未加载
hizanberg9 个月前
Anyone else used Hono with SSR JSX? [1]<p>Was super productive and easy to create a Cloudflare Worker Web App that’s free to host thanks to Cloudflare’s generous 100k daily worker request limit.<p>Generally don’t believe in serverless for larger Apps, but for small websites that you just want to create, deploy and ignore - it’s great!<p><a href="https:&#x2F;&#x2F;hono.dev&#x2F;docs&#x2F;guides&#x2F;jsx" rel="nofollow">https:&#x2F;&#x2F;hono.dev&#x2F;docs&#x2F;guides&#x2F;jsx</a>
mg9 个月前
What is the benefit of mixing js and html?<p><pre><code> el = &lt;button&gt;Click me&lt;&#x2F;button&gt; as HTMLButtonElement; </code></pre> What would be the downside of<p><pre><code> el = html.button(&#x27;&lt;button&gt;Click me&lt;&#x2F;button&gt;&#x27;); </code></pre> ?<p>That way no compilation step would be needed and debugging would be easier as the code executed in the browser is the same code the developer writes.
评论 #41273316 未加载
评论 #41272782 未加载
EugeneOZ9 个月前
As often happens with minimalistic approaches, it only looks interesting on very small and very simple examples.<p>After “How would they handle large data?” it turns into an unreadable mess.<p>Communication between elements is not covered, global deps, DOM updates scheduling, content projection, and so on - you “just don&#x27;t need it” in small demo examples, but you do need it in the real apps.
drikerf9 个月前
Nice project! I do wonder though if jsx is the best way to represent elements in code?<p>Clojure datastructures makes this so much more enjoyable. Everything is just basic lists and maps which makes it very flexible and powerful.<p>[:ul [:li &quot;task 1&quot;] [:li &quot;task 2&quot;]]<p>It&#x27;s weird that it&#x27;s not more common for making web apps.
评论 #41275550 未加载
评论 #41272507 未加载
whazor9 个月前
I don&#x27;t see why the type casting (as HTMLButtonElement) is needed. Because document.createElement(&quot;button&quot;) returns HTMLButtonElement in TypeScript.
ilrwbwrkhv9 个月前
Imba is what anyone interested in this sort of thing should look at. I have no idea why it is not more popular. Maybe because JS devs falls for Faang marketing easily.<p><a href="https:&#x2F;&#x2F;imba.io&#x2F;" rel="nofollow">https:&#x2F;&#x2F;imba.io&#x2F;</a>
评论 #41272853 未加载
andruc9 个月前
It&#x27;s very strange that when I land on the page for the very first time, I land halfway down the page and I&#x27;m staring at a block of random code.<p>Not what you&#x27;d expect to see.
评论 #41274500 未加载
talkingtab9 个月前
A side question. The advantage of JSX I see is the ability to connect, declaratively, components. I find this very helpful in terms of understanding programs I write. I wonder if I use React not because of the virtual dom, but simply because of JSX.<p>So I would like to explore the ability to use JSX in non-DOM environments. react-three-fiber does this with Threejs, but then it is still React oriented. I found this article about parsing JSX <a href="https:&#x2F;&#x2F;blog.bitsrc.io&#x2F;demystifying-jsx-building-your-own-jsx-parser-from-scratch-caecf58d7cbd" rel="nofollow">https:&#x2F;&#x2F;blog.bitsrc.io&#x2F;demystifying-jsx-building-your-own-js...</a>. And I know babel has something that parses JSX.<p>Does anyone have recommendations for doing this. Threejs to me a good candidate - a non React version, since it is a hierarchical system (scene, meshes, materials etc), but I suspect there are other applications.<p>I made an attempt to implement a Javascript version of Hickey&#x27;s transducers - a sort of conveyor belt of functions and that is another instance of a series of processing steps that might be best represented in JSX
评论 #41275413 未加载
andrewstuart9 个月前
Just out of interest I wanted to see something a little bit similar in Web Components:<p><pre><code> &lt;html lang=&quot;en&quot;&gt; &lt;body&gt; &lt;h1&gt;Web Components Examples&lt;&#x2F;h1&gt; &lt;h2&gt;Counter Component&lt;&#x2F;h2&gt; &lt;counter-component&gt;&lt;&#x2F;counter-component&gt; &lt;h2&gt;Clickable Button Component&lt;&#x2F;h2&gt; &lt;clickable-button&gt;&lt;&#x2F;clickable-button&gt; &lt;h2&gt;Toggler Component&lt;&#x2F;h2&gt; &lt;toggler-component&gt;&lt;&#x2F;toggler-component&gt; &lt;script&gt; class CounterComponent extends HTMLElement { constructor() { super(); this.count = 0; this.button = document.createElement(&#x27;button&#x27;); this.button.textContent = this.count; this.button.addEventListener(&#x27;click&#x27;, () =&gt; { this.count++; this.button.textContent = this.count; }); this.attachShadow({ mode: &#x27;open&#x27; }).appendChild(this.button); } } class ClickableButton extends HTMLElement { constructor() { super(); this.clicked = false; this.button = document.createElement(&#x27;button&#x27;); this.button.textContent = &quot;Click me!&quot;; this.button.addEventListener(&#x27;click&#x27;, () =&gt; { this.clicked = !this.clicked; this.button.textContent = this.clicked ? &quot;Clicked!&quot; : &quot;Click me!&quot;; }); this.attachShadow({ mode: &#x27;open&#x27; }).appendChild(this.button); } } class TogglerComponent extends HTMLElement { constructor() { super(); this.on = false; this.button = document.createElement(&#x27;button&#x27;); this.button.textContent = &quot;OFF&quot;; this.button.addEventListener(&#x27;click&#x27;, () =&gt; { this.on = !this.on; this.button.textContent = this.on ? &quot;ON&quot; : &quot;OFF&quot;; }); this.attachShadow({ mode: &#x27;open&#x27; }).appendChild(this.button); } } customElements.define(&#x27;counter-component&#x27;, CounterComponent); customElements.define(&#x27;clickable-button&#x27;, ClickableButton); customElements.define(&#x27;toggler-component&#x27;, TogglerComponent); &lt;&#x2F;script&gt; &lt;&#x2F;body&gt; &lt;&#x2F;html&gt;</code></pre>
NohatCoder9 个月前
For anyone who can live without &lt;&gt; syntax I made DOM Maker, no compilation step, no injection vulnerability footguns, just make a bunch of function calls in a tree structure, and you get DOM with the same tree structure, complete with non-string event handlers.<p>Mostly I just do Vanilla.js, but the vanilla DOM creation functions turn really verbose, I got tired of that and created this to cut back on code size and increase readability.<p>There are other libraries that do something similar, but in my own very biased opinion this is one of the better.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;NoHatCoder&#x2F;DOM_Maker">https:&#x2F;&#x2F;github.com&#x2F;NoHatCoder&#x2F;DOM_Maker</a>
cies9 个月前
I frown at JSX. Just a layer of abstraction that is so &quot;leaky&quot; that you have to know what actually goes on in the layers below or you are fucked.<p>It looks simpler at first glance&#x2F; to a untrained eye; but it&#x27;s just adding complexity without really solving any problems.<p>I like approaches like Kotlinx.html, scalatags, Elm&#x27;s HTML package or HtmlFlow. They are also abstractions, but they add typesafety that html-as-a-string does not offer. On top of that you get breakpoints, code completion, and you can keep working in one language.
miika9 个月前
I used to explore similar stuff and prototyped something I call “Vanilla Components” but then in the end I fell in love with Web Components and quit React (and all other frameworks).
spullara9 个月前
I was bummed when they removed E4X from the browser implementations.
arjvik9 个月前
What benefit does the virtual DOM add?
评论 #41271379 未加载
评论 #41270960 未加载
评论 #41270866 未加载
评论 #41270946 未加载
评论 #41271084 未加载
评论 #41271192 未加载
xwall9 个月前
No matter how complex your app is but still React will not break, performance on web is not a big issue as benchmarks say, even a junior developer can achieve 90%+ lighthouse score, but any senior developer may fail to ship it successfully.<p>ultimately go to react.dev because: &quot;Maturing is realizing React is best&quot;
dqh9 个月前
Those interested in this space may find my fairly unknown project interesting: <a href="https:&#x2F;&#x2F;nakedjsx.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;nakedjsx.org&#x2F;</a><p>It started as a static site generator but added a bunch of support for client JavaScript too.
NaN13529 个月前
I’m having fun using vanilla js with lit-html. Using string templates instead of jsx. VSCode extensions for lit make it almost identical to editing vue templates with type checking etc
waynenilsen9 个月前
This plays very nicely with the locality of behavior model of htmx
n3storm9 个月前
For me is like old PHP where HTML and controlling and data access was all around. We use to call it spaghetti code.
cyanydeez9 个月前
I just don&#x27;t understand how people can configure their brains to parse html inside JavaScript
评论 #41272542 未加载
评论 #41272288 未加载
评论 #41272865 未加载
emadda9 个月前
One of the reasons for JSX originally was to reduce usage of the DOM APIs, as they are slower than direct JS object manipulation. The JSX diff of prev&#x2F;next allows you to minimize DOM API calls.<p>I would guess there is more overhead in creating a dom element than a JS object (which JSX elements compile to).
评论 #41274968 未加载
nf179 个月前
Great job, is there something similar but for SwiftUI?
nashashmi9 个月前
I wonder what The examples would look like in ECMAscript 5.
NaN13529 个月前
How does this stack up aginst using lit-html?
andruc9 个月前
Any comparisons on performance?
frabjoused9 个月前
It’s already solved. It works well. Just walk away.
jwtorres9 个月前
I genuinely don&#x27;t understand why anyone would be interested in using frameworks on top of JS. None of them can do anything that pure JS can&#x27;t do (+libraries), they just make it less readable and less intuitive compared to the original C-like syntax of JS. JS libraries make sense, of course, but why keep messing with the syntax?