React” in 70 lines of vanilla TypeScript

栏目: IT技术 · 发布时间: 4年前

内容简介:Noact is a minimalEven has support for

Re Noact

Noact is a minimal self-rendering Virtual DOM library.

  • Declarative: Pretty much like React, without the JSX compilation of course, hence the name.
  • Type safe: Noact is completely typesafe, which means you get static type checking for free!
  • Simple: Only 60 lines of type declarations & rendering code. (and 10ish lines of code-gen code)

Example App

How it feels to write Noact

React” in 70 lines of vanilla TypeScript

- Explosions -

Even has support for style auto complete

React” in 70 lines of vanilla TypeScript

Usage

Noact is inspired by the syntax of the elm HTML engine

import { button, div } from "./NoactElements"
const component1 = div({},
    button({ onclick: () => alert(":D"), txt: "+" }),
    div({ txt: "♥" }),
    button({ onclick: () => alert("D:"), txt: "-" })
)

component1 is a memoized () => HTMLElement function, component1() will give you back

<div>
  <button>+</button>
  <div>♥</div>
  <button>-</button>
</div>

You can use component1 as it is, or compose it in a Virtual DOM configuration

import { createMountPoint } from "./Noact"
const mount = createMountPoint(document.querySelector(`#root`))
const remount = () => mount(
    component1,
    span({ txt: new Date().toString() })
)
setInterval(remount, 1000)

Here the root element will be populated with both component1 and span . Every 1000ms, #root > span and only #root > span will be updated.

In essence, component1 is both the rendering function, and the virtual DOM.

License

MIT License


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

颠覆医疗

颠覆医疗

[美]埃里克·托普 / 张南、魏薇、何雨师 / 译言·东西文库/电子工业出版社 / 2014-1-20 / 55.00

“创造性破坏”是奥地利经济学家约瑟夫·熊彼特最著名的理论,当一个产业在革新之时,都需要大规模地淘汰旧的技术与生产体系,并建立起新的生产体系。电器之于火器、汽车之于马车、个人计算机之于照排系统,都是一次又一次的“创造性破坏”,旧的体系完全不复存在,新的体系随之取代。 “创造性破坏”已经深深地改变了我们的生活,在这个数字时代,我们身边的一切都被“数字化”了。只有一处,也许是由于其本身的根深蒂固,......一起来看看 《颠覆医疗》 这本书的介绍吧!

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具