微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

fre 安装使用props 小而美的前端框架

程序名称:fre 安装使用props

授权协议: MIT

操作系统: 跨平台

开发语言: JavaScript

fre 安装使用props 介绍

Fre (发音/fri:/, like free) 是一个小而美的前端框架,实现了 Concurrent 和 Suspense

其实,free 是一部动漫名,也是我最喜欢的番没有之一,haru 是我儿子! 参见 c

特性:

  • 函数式组件与 hooks API
  • 并发与 Suspense
  • keyed reconcilation algorithm

安装

yarn add fre -S

使用

import { render, h, useState } from 'fre'

function Counter() {
  const [count, setCount] = useState(0)
  return (
    <div>
      <h1>{count}</h1>
      <button onClick={() => setCount(count + 1)}>+</button>
    </div>
  )
}

render(<Counter />, document.getElementById('app'))
import { render, useState, h } from "fre"

function Sex() {
  const [sex, setSex] = useState("boy")
  return (
    <div class="sex">
      <button onClick={() => setSex(sex === "boy" ? "girl" : "boy")}>x</button>
      <Counter sex={sex} />
    </div>
  )
}

function Counter(props) {
  const [count, setCount] = useState(0)
  return (
    <div class="counter">
      <h1>{props.sex}</h1>
      <button onClick={() => setCount(count + 1)}>+</button>
      <h1>{count}</h1>
    </div>
  )
}

render(<Sex />, document.getElementById("app"))

props

虽然 hooks API 使得 state 在 function 内部受控,但是 props 仍然是这个组件从外部接受的√

如下,sex 就是从父组件传下来的

function Counter(props) {
  const [count, setCount] = useState(0)
  return (
    <div class="counter">
      <h1>{props.sex}</h1>
      <button onClick={() => setCount(count + 1)}>+</button>
      <h1>{count}</h1>
    </div>
  )
}

认也对外暴露了 h 函数,可以选用 JSX

import { h } from 'fre'

webpack 需配置:

{
  "plugins": [
    ["transform-react-jsx", { "pragma":"Fre.h" }]
  ]
}

基于 fiber 链表的 diff 方案,使用 hash 标记位置,更方便的比对

Concurrent

异步渲染,通过时间切片,suspense 的方式,对任务进行优先级调度,打断继续任务

fre 安装使用props 官网

https://fre.js.org

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐