0.0.37 • Published 4 years ago

js-preactive v0.0.37

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

js-preactive

A R&D project to evaluate an alternative API for developing components and hook functions with Preact. The main advantages of the new API are:

  • 0% magic
  • Does not make any trouble for the garbage collector
  • No rules of hooks
  • No special linter necessary
  • 100% accurately typeable - in the function type signature of hook functions or function that generate hook functions etc., it will always be visible what we are dealing with.

Installation

git clone https://github.com/js-works/js-preactive.git
cd preactive
npm install

Running demos

npm run storybook

Examples

Stateless components

import { h, render } from 'preact'
import { statelessComponent } from 'js-preactive'

const HelloWorld = statelessComponent('HelloWorld', props => {
  return (
    <div>
      {props.salutation || 'Hello'}, {props.name || 'world'}
    </div>
  )
})

Stateful components

import { h, render } from 'preact'
import { statefulComponent, useState } from 'js-preactive'

const Counter = statefulComponent('Counter', (c, props) => {
  const
    [state, setState] = useState(c, { count: props.initialCount || 0 }),
    onIncrement = () => setState('count', it => it + 1)

  return () =>
    <div>
      <label>{props.label || 'Counter'}: </label>
      <button onClick={onIncrement}>{state.count}</button>
    </div>
})

render(<Counter/>, document.getElementById('app'))

In the above examples the c is a so called component controller (some kind of representation for the component instance). The type of the component controller is currently the following (please be aware that "normal" developers will never have to use these methods directly they will only be used internally by some basic hook and utility functions):

type Ctrl<P extends Props = {}> = {
  getDisplayName(): string,
  getProps(): P,
  isInitialized(): boolean,
  isMounted(): boolean,
  refresh(runOnceBeforeRender?: () => void): void,
  getContextValue<T>(context: Context<T>): T,
  afterMount(subscriber: Subscriber): void,
  afterUpdate(subscriber: Subscriber): void,
  beforeUnmount(subscriber: Subscriber): void,
}

type Props = Record<string, any>
type Subscriber = () => void
type Context<T> = Preact.Context<T>

Additional example - showing some more features

import { h, render } from 'preact'
import { statefulComponent, useEffect, useProps, useValue } from 'js-preactive'

const Counter = statefulComponent('Counter', c => {
  const
    props = useProps(c, { initialCount: 0, label: 'Counter' }),
    [count, setCount] = useValue(c, props.initialCount),
    onIncrement = () => setCount(it => it + 1)

  useEffect(c, () => {
    console.log(`"${props.label}" has been mounted`)

    return () => console.log(`Unmounting "${props.label}"`)
  }, null)

  useEffect(c, () => {
    console.log(`Value of "${props.label}": ${count.value}`)
  }, () => [count.value])

  return () =>
    <div>
      <label>{props.label}: </label>
      <button onClick={onIncrement}>{count.value}</button>
    </div>
})

render(<Counter/>, document.getElementById('app'))

API

Component definition

  • statelessComponent(displayName, render: props => vnode)
  • statefulComponent(displayName, init: c => props => vnode)

Utility functions

  • isMounted(c)
  • forceUpdate(c)
  • getContextValue(c, context, defaultValue?)
  • asRef(valueOrRef)
  • toRef(getter)

Hooks

  • useState(c, initialState)
  • useValue(c, initialCount)
  • useMemo(c, calculation, () => dependencies)
  • useContext(c, context)
  • useEffect(c, action, () => dependencies)
  • useInterval(c, action, milliseconds)

Project state

This R&D project is in a very early development state

0.0.37

4 years ago

0.0.34

4 years ago

0.0.35

4 years ago

0.0.36

4 years ago

0.0.32

4 years ago

0.0.31

4 years ago

0.0.30

4 years ago

0.0.29

4 years ago

0.0.28

4 years ago

0.0.26

4 years ago

0.0.27

4 years ago

0.0.25

4 years ago

0.0.23

4 years ago

0.0.22

4 years ago

0.0.20

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.16

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago