# 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:

Latest version **0.0.66** (published 2022-08-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install preactive
pnpm add preactive
yarn add preactive
bun add preactive
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.66 |
| Published | 2022-08-05 |
| First published | 2019-12-21 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 317.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | mcjazzyfunky, js-works |

## Links

- npm: https://www.npmjs.com/package/preactive
- Repository: https://github.com/js-works/preactive
- Homepage: https://github.com/js-works/preactive#readme
- Issues: https://github.com/js-works/preactive/issues
- npm.io page: https://npm.io/package/preactive

## Dependencies (2)

- [mobx](https://npm.io/package/mobx.md) ^6.6.1
- [preact](https://npm.io/package/preact.md) ^10.10.0

## Recent versions

- 0.0.66 (latest) — 2022-08-05
- 0.0.42 — 2022-07-28
- 0.0.16 — 2022-07-27
- 0.0.15 — 2019-12-21

## README

# preactive

A R&D project to evaluate an alternative API for developing components
with Preact using an alternative to hook functions (called "extensions").<br>
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

```tsx
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

```tsx
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

---
_Source: https://npm.io/package/preactive · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
