fre
TypeScript icon, indicating that this package has built-in type declarations

2.8.3 • Public • Published

fre logo

Fre

👻 Tiny Concurrent UI library with Fiber.

GitHub License Build Status Code Coverage npm-v npm-d brotli

  • Concurrent Mode — This is an amazing idea, which implements the coroutine scheduler in JavaScript, it also called Time slicing.

  • Keyed reconcilation algorithm — Fre has a minimal diff algorithm, It supported keyed, pre-process, offscreen rendering and hydrate.

  • Do more with less — Fre get the tiny size, but it has most features, virtual DOM, hooks API, Suspense, Fragments, Fre.memo and so on.

Contributors

Usage

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)

Features

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>
}

useContext

import { createContext, useContext } from 'react';

const ThemeContext = createContext(null);

function App() {
  return (
    <ThemeContext.Provider value="dark">
      <Button />
    </ThemeContext.Provider>
  )
}

function Button({ children }) {
  const theme = useContext(ThemeContext);
  const className = 'button-' + theme;
  return (
    <button class={className}>
      {children}
    </button>
  );
}

Suspense

const Hello = lazy('./hello.js')

export function App() {
  return (
    <div>
      <Suspense fallback={<div>loading...</div>}>
        <Hello />
        <div>world!</div>
      </Suspense>
    </div>
  )
}

Fragments

// fragment
function App() {
  return <>{something}</>
}
// render array
function App() {
  return [a, b, c]
}

jsx2

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

License

MIT @yisar

FOSSA Status

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.8.366latest

Version History

VersionDownloads (Last 7 Days)Published
2.8.366
2.8.259
2.8.14
2.8.00
2.7.65
2.7.50
2.7.40
2.7.30
2.7.20
2.7.10
2.7.00
2.6.70
2.6.55
2.6.41
2.6.31
2.6.21
2.6.11
2.6.013
2.5.61
2.5.53
2.5.21
2.5.19
2.5.02
2.4.50
2.4.45
2.4.33
2.4.20
2.4.10
2.4.00
2.3.40
2.3.30
2.3.20
2.3.10
2.3.00
2.2.10
2.2.00
2.1.20
2.1.10
2.1.00
2.1.0-beta40
2.1.0-beta30
2.1.0-beta20
2.1.0-beta10
2.1.0-alpha50
2.1.0-alpha40
2.1.0-alpha30
2.1.0-alpha20
2.1.0-alpha0
2.0.71
2.0.60
2.0.50
2.0.40
2.0.30
2.0.20
2.0.10
2.0.02
2.0.0-rc.170
2.0.0-rc.160
2.0.0-rc.150
2.0.0-rc.140
2.0.0-rc.130
2.0.0-rc.120
2.0.0-rc.110
2.0.0-rc.100
2.0.0-rc90
2.0.0-rc80
2.0.0-rc70
2.0.0-rc.60
2.0.0-rc.50
2.0.0-rc.40
2.0.0-rc.30
2.0.0-rc.20
2.0.0-rc.10
2.0.0-rc0
2.0.0-beta.30
2.0.0-beta.20
2.0.0-beta.10
2.0.0-alpha.50
2.0.0-alpha.40
2.0.0-alpha.30
2.0.0-alpha.20
2.0.0-alpha.10
1.13.80
1.13.70
1.13.60
1.13.50
1.14.00
1.13.40
1.13.31
1.13.20
1.13.10
1.13.02
1.12.60
1.12.50
1.12.40
1.12.30
1.12.20
1.12.10
1.12.00
1.11.110
1.11.100
1.11.80
1.11.70
1.11.61
1.11.50
1.11.40
1.11.30
1.11.21
1.11.11
1.11.03
1.10.20
1.10.00
1.9.90
1.9.80
1.9.70
1.9.60
1.9.50
1.9.40
1.9.30
1.9.20
1.9.10
1.9.00
1.8.80
1.8.70
1.8.62
1.8.50
1.8.40
1.8.30
1.8.20
1.8.11
1.8.00
1.7.140
1.7.130
1.7.120
1.7.110
1.7.100
1.7.90
1.7.84
1.7.70
1.7.60
1.7.50
1.7.40
1.7.30
1.7.20
1.7.10
1.7.00
1.6.60
1.6.51
1.6.30
1.6.20
1.6.11
1.6.00
1.5.120
1.5.110
1.5.100
1.5.91
1.5.80
1.5.70
1.5.60
1.5.50
1.5.40
1.5.30
1.5.20
1.5.10
1.5.00
1.4.90
1.4.80
1.4.70
1.4.61
1.4.50
1.4.40
1.4.30
1.4.20
1.4.10
1.4.00
1.3.160
1.3.150
1.3.140
1.3.130
1.3.120
1.3.110
1.3.100
1.3.90
1.3.80
1.3.70
1.3.60
1.3.50
1.3.40
1.3.30
1.3.20
1.3.10
1.3.00
1.2.80
1.2.70
1.2.60
1.2.50
1.2.20
1.2.10
1.2.00
1.1.10
1.1.00
1.0.10
1.0.00
1.0.0-alpha10
1.0.0-beta60
1.0.0-beta50
1.0.0-beta40
1.0.0-beta30
1.0.0-beta20
1.0.0-beta0
0.5.00
0.1.10
0.1.00
0.0.70
0.0.60
0.0.50
0.0.40
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i fre

Homepage

fre.js.org

Weekly Downloads

203

Version

2.8.3

License

MIT

Unpacked Size

117 kB

Total Files

29

Last publish

Collaborators

  • yse