GithubHelp home page GithubHelp logo

han3jim / fre Goto Github PK

View Code? Open in Web Editor NEW

This project forked from frejs/fre

0.0 0.0 0.0 6.11 MB

:ghost: Tiny Concurrent UI library with Fiber.

Home Page: https://fre.deno.dev

License: MIT License

JavaScript 1.79% TypeScript 98.21%

fre's Introduction

fre logo

Fre

πŸ‘» Tiny Concurrent UI library with Fiber.

Build Status Code Coverage npm-v npm-d brotli

  • Concurrent with Fiber β€” This is an amazing idea, which implements the coroutine scheduler in JavaScript, and the rendering is asynchronous, which supports Time slicing and Suspense.

  • Highly-optimized algorithm β€” Fre has a better reconciliation algorithm, which traverses from both ends with O (n) complexity, and supports keyed.

  • Do more with less β€” After tree shaking, project of hello world is only 2KB, but it has most features, virtual DOM, hooks API, Fragment, ErrorBoundary and more.

Use

yarn add fre
import { render, useState } from 'fre'

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

render(<App />, document.body)

Hooks API

useState

useState is a base API, It will receive initial state and return an Array

You can use it many times, new state is available when component is rerender

function App() {
  const [up, setUp] = useState(0)
  const [down, setDown] = useState(0)
  return (
    <>
      <h1>{up}</h1>
      <button onClick={() => setUp(up + 1)}>+</button>
      <h1>{down}</h1>
      <button onClick={() => setDown(down - 1)}>-</button>
    </>
  )
}

useReducer

useReducer and useState are almost the same,but useReducer needs a global reducer

function reducer(state, action) {
  switch (action.type) {
    case 'up':
      return { count: state.count + 1 }
    case 'down':
      return { count: state.count - 1 }
  }
}

function App() {
  const [state, dispatch] = useReducer(reducer, { count: 1 })
  return (
    <>
      {state.count}
      <button onClick={() => dispatch({ type: 'up' })}>+</button>
      <button onClick={() => dispatch({ type: 'down' })}>-</button>
    </>
  )
}

useEffect

It is the execution and cleanup of effects, which is represented by the second parameter

useEffect(f)       //  effect (and clean-up) every time
useEffect(f, [])   //  effect (and clean-up) only once in a component's life
useEffect(f, [x])  //  effect (and clean-up) when property x changes in a component's life
function App({ flag }) {
  const [count, setCount] = useState(0)
  useEffect(() => {
    document.title = 'count is ' + count
  }, [flag])
  return (
    <>
      <h1>{count}</h1>
      <button onClick={() => setCount(count + 1)}>+</button>
    </>
  )
}

If it returns a function, the function can do cleanups:

useEffect(() => {
  document.title = 'count is ' + count
  return () => {
    store.unsubscribe()
  }
}, [])

useLayout

More like useEffect, but useLayout is sync and blocking UI.

useLayout(() => {
  document.title = 'count is ' + count
}, [flag])

useMemo

useMemo has the same rules as useEffect, but useMemo will return a cached value.

const memo = (c) => (props) => useMemo(() => c, [Object.values(props)])

useCallback

useCallback is based useMemo, it will return a cached function.

const cb = useCallback(() => {
  console.log('cb was cached.')
}, [])

useRef

useRef will return a function or an object.

function App() {
  useEffect(() => {
    console.log(t) // { current:<div>t</div> }
  })
  const t = useRef(null)
  return <div ref={t}>t</div>
}

If it uses a function, it can return a cleanup and executes when removed.

function App() {
  const t = useRef((dom) => {
    if (dom) {
      doSomething()
    } else {
      cleanUp()
    }
  })
  return flag && <span ref={t}>I will removed</span>
}

Suspense

This is another feature of concurrent rendering, which can achieve asynchronous refresh without the aid of state.

const LazyComponent = lazy(Component)

function App() {
  return <Suspense fallback={<div>Loading...</div>}>
    <LazyComponent/>
  </Suspense>
}

ErrorBoundary

Similar to Suspense is ErrorBoundary, where rendering can fallback when errors are caught.

function App() {
  return <ErrorBoundary fallback={(e)=>{
    console.error(e)
    return e
  }}>
    <ErrorComponent/>
  </ErrorBoundary>
}
function ErrorComponent(){
  throw 'err'
}

jsx2

plugins: [
  [
    '@babel/plugin-transform-react-jsx',
    {
      runtime: 'automatic',
      importSource: 'fre',
    },
  ],
]

Compare with other frameworks

The comparison is difficult because the roadmap and trade-offs of each framework are different, but we have to do so.

  • react

React is the source of inspiration for fre. Their implementation and asynchronous rendering are similar. The most amazing thing is concurrent mode, which means that react and fre have the same roadmap -- Exploring concurrent use cases.

But at the same time, fre has obvious advantages in reconciliation algorithm and bundle size.

  • vue / preact

To some extent, vue and preact are similar. They have similar synchronous rendering, only the API is different.

The reconciliation algorithm of fre is similar to vue, but the biggest difference is that vue/preact do not support concurrent mode, this means that the roadmap is totally different.

framework concurrent reconcilation algorithm bundle size
fre2 √ β˜…β˜…β˜…β˜… 2kb
react17 √ β˜…β˜… 39kb
vue3 Γ— β˜…β˜…β˜…β˜…β˜… 30kb
preactX Γ— β˜…β˜…β˜…β˜… 4kb

License

MIT @yisar

FOSSA Status

fre's People

Contributors

yisar avatar mindplay-dk avatar dependabot[bot] avatar zhongmeizhi avatar wu-yu-xuan avatar winesu avatar juojuo avatar chenjigeng avatar ahaoboy avatar zhmushan avatar prettykernel avatar 1123612483 avatar malash avatar yingpengsha avatar xyuu avatar microj avatar yiliang114 avatar kukac7 avatar timetravelcyn avatar duhongjun avatar fossabot avatar hyrious avatar lishaoxin123 avatar conwnet avatar shadowvzs avatar ederzz avatar fantasticsoul avatar 1742284240 avatar ashmit4 avatar symind avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.