0.0.66 • Published 2 years ago

preactive v0.0.66

Weekly downloads
4
License
ISC
Repository
github
Last release
2 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

2 years ago

0.0.40

2 years ago

0.0.63

2 years ago

0.0.41

2 years ago

0.0.64

2 years ago

0.0.42

2 years ago

0.0.65

2 years ago

0.0.43

2 years ago

0.0.66

2 years ago

0.0.45

2 years ago

0.0.46

2 years ago

0.0.47

2 years ago

0.0.60

2 years ago

0.0.16

2 years ago

0.0.39

2 years ago

0.0.51

2 years ago

0.0.52

2 years ago

0.0.53

2 years ago

0.0.54

2 years ago

0.0.55

2 years ago

0.0.56

2 years ago

0.0.57

2 years ago

0.0.58

2 years ago

0.0.50

2 years ago

0.0.48

2 years ago

0.0.49

2 years ago

0.0.15

4 years ago

0.0.13

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