TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: TinyJs React like framework in 35 lines of code

43 pointsby atum474 months ago
Hi HN, I got to work yesterday and today and came up with a very simple way to create and manage state in TinyJS.<p>I end up creating a simple App to illustrate how to use the new createState function and custom components in a React like manner. Here&#x27;s the code for it - <a href="https:&#x2F;&#x2F;github.com&#x2F;victorqribeiro&#x2F;tinyapp">https:&#x2F;&#x2F;github.com&#x2F;victorqribeiro&#x2F;tinyapp</a><p>Here&#x27;s the PR for the createState function - <a href="https:&#x2F;&#x2F;github.com&#x2F;victorqribeiro&#x2F;TinyJS&#x2F;pull&#x2F;9">https:&#x2F;&#x2F;github.com&#x2F;victorqribeiro&#x2F;TinyJS&#x2F;pull&#x2F;9</a>

5 comments

chrismorgan4 months ago
A comment on code organisation.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;victorqribeiro&#x2F;TinyApp&#x2F;blob&#x2F;db92225cfd0aed935b7dd6f70e36c0a852a5e462&#x2F;js&#x2F;Input.js">https:&#x2F;&#x2F;github.com&#x2F;victorqribeiro&#x2F;TinyApp&#x2F;blob&#x2F;db92225cfd0ae...</a>:<p><pre><code> export default function Input(state) { const inp = input({ &#x2F;&#x2F; ① type: &#x27;number&#x27;, value: state.count, onchange }) function update() { &#x2F;&#x2F; ② inp.value = state.count } function onchange() { &#x2F;&#x2F; ③ state.count = parseInt(this.value) } state.addUpdate(&#x27;count&#x27;, update) &#x2F;&#x2F; ④ return inp } </code></pre> (Labelling comments mine.)<p>This ordering isn’t great: ① refers to ③, and ④ refers to ②. It’d be better to reorder them, <i>at least</i> to swap ② and ③, and remove the blank line separating ② and ④:<p><pre><code> export default function Input(state) { const inp = input({ &#x2F;&#x2F; ① type: &#x27;number&#x27;, value: state.count, onchange }) function onchange() { &#x2F;&#x2F; ③ state.count = parseInt(this.value) } function update() { &#x2F;&#x2F; ② inp.value = state.count } state.addUpdate(&#x27;count&#x27;, update) &#x2F;&#x2F; ④ return inp } </code></pre> But you really can do <i>much</i> better:<p><pre><code> export default function Input(state) { const inp = input({ type: &#x27;number&#x27;, value: state.count, onchange() { state.count = parseInt(this.value) } }) state.addUpdate(&#x27;count&#x27;, function() { inp.value = state.count }) return inp } </code></pre> At least at this scale, that’s <i>much</i> easier to follow.<p>Inlining can improve other places too (though at larger scales it becomes more debatable):<p><pre><code> export default function Button(state) { return button({ onclick() { state.count += 1 }, style: { color: &#x27;black&#x27;, border: &#x27;solid 1px black&#x27; }, }, &#x27;+ 1&#x27;) }</code></pre>
评论 #42880989 未加载
评论 #42877783 未加载
评论 #42875881 未加载
评论 #42876473 未加载
bolfe114 months ago
I created a typescript version of your framework<p><a href="https:&#x2F;&#x2F;github.com&#x2F;guilhermebolfe11&#x2F;TinyTS">https:&#x2F;&#x2F;github.com&#x2F;guilhermebolfe11&#x2F;TinyTS</a>
评论 #42881992 未加载
leontrolski4 months ago
33 line React <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=22776753">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=22776753</a>
campak4 months ago
This is cool. Keep up the good work and enjoy tinkering!
codingdave4 months ago
So, if you are building this off &#x27;window&#x27;, you are basically just adding syntax sugar to one big global variable. That is absolutely a simple solution to storing state. In one place. But that is not comparable to the functionality of React, so I&#x27;m not sure I agree that it is a &quot;react-like framework&quot;.
评论 #42834566 未加载
评论 #42876297 未加载
评论 #42875509 未加载