0.0.66 • Published 3 years ago

preactive v0.0.66

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

preactive

A R&D project to evaluate an alternative API for developing components with Preact using an alternative to hook functions (called "extensions"). The main advantages of the new API are:

  • No rules of hooks
  • No special linter necessary
  • 100% accurately typeable

Be aware that this project is just for research purposes and is not meant to be used in production.

Installation

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

Running demos

npm run storybook

Examples

Simple counter

import { h, render } from 'preact';
import { component, preset } from 'preactive';
import { stateVal } from 'preactive/ext';

const Counter = component('Counter')<{
  initialCount?: number;
  label?: string;
}>((props) => {
  preset(props, () => ({
    initialCount: 0,
    label: 'Counter'
  }));

  const [getCount, setCount] = stateVal(props.initialCount);
  const onIncrement = () => setCount((it) => it + 1);

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

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

Additional example - showing some more features

import { h, render } from 'preact';
import { component, preset } from 'preactive';
import { effect, stateObj } from 'preactive/ext';

const Counter = component('Counter')<{
  initialCount?: number;
  label?: string;
}>((props) => {
  preset(props, () => ({
    initialCount: 0,
    label: 'Counter'
  }));

  const [state, set] = stateObj({
    count: props.initialCount
  });

  const onIncrement = () => set.count((it) => it + 1);

  effect(
    () => console.log(`Value of "${props.label}": ${state.count}`),
    () => [state.count]
  );

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

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

API

Core functions

  • component(displayName, render: props => vnode): ComponentClass
  • component(displayName, init: props => () => vnode): ComponentClass
  • component(displayName): (render: props => vnode) => ComponentClass
  • component(displayName): (init: props => () => vnode) => ComponentClass
  • preset(props, defaultProps or getDefaultProps)

Utility functions

  • tbd

Extensions

  • stateVal(initialValue)
  • stateObj(initialValues)
  • createMemo(calculation, getDependencies)
  • consume(context)
  • effect(action, getDependencies? | null)
  • interval(action, milliseconds)
  • handlePromise(getPromise)

Project state

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

0.0.62

3 years ago

0.0.40

3 years ago

0.0.63

3 years ago

0.0.41

3 years ago

0.0.64

3 years ago

0.0.42

3 years ago

0.0.65

3 years ago

0.0.43

3 years ago

0.0.66

3 years ago

0.0.45

3 years ago

0.0.46

3 years ago

0.0.47

3 years ago

0.0.60

3 years ago

0.0.16

3 years ago

0.0.39

3 years ago

0.0.51

3 years ago

0.0.52

3 years ago

0.0.53

3 years ago

0.0.54

3 years ago

0.0.55

3 years ago

0.0.56

3 years ago

0.0.57

3 years ago

0.0.58

3 years ago

0.0.50

3 years ago

0.0.48

3 years ago

0.0.49

3 years ago

0.0.15

5 years ago

0.0.13

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago