# microcomponent

> Smol event based component library

Latest version **3.1.6** (published 2017-06-13) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.6 |
| Published | 2017-06-13 |
| First published | 2017-04-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 35 |
| Maintainers | juliangruber, yoshuawuyts |
| Keywords | component, event, diffing |

## Links

- npm: https://www.npmjs.com/package/microcomponent
- Repository: https://github.com/yoshuawuyts/microcomponent
- Homepage: https://github.com/yoshuawuyts/microcomponent#readme
- Issues: https://github.com/yoshuawuyts/microcomponent/issues
- npm.io page: https://npm.io/package/microcomponent

## Dependencies (4)

- [nanomorph](https://npm.io/package/nanomorph.md) ^5.0.0
- [nanologger](https://npm.io/package/nanologger.md) ^1.1.0
- [nanocomponent](https://npm.io/package/nanocomponent.md) ^5.0.0
- [juliangruber-shallow-equal](https://npm.io/package/juliangruber-shallow-equal.md) ^1.0.4

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 3.1.6 (latest) — 2017-06-13
- 3.1.5 — 2017-05-23
- 3.1.4 — 2017-05-23
- 3.1.1 — 2017-05-01
- 3.1.0 — 2017-04-15
- 3.0.2 — 2017-04-14
- 3.0.1 — 2017-04-14
- 3.0.0 — 2017-04-14
- 2.0.1 — 2017-04-08
- 2.0.0 — 2017-04-07
- 1.1.0 — 2017-04-07
- 1.0.0 — 2017-04-01

## README

# microcomponent [![stability][0]][1]
[![npm version][2]][3] [![build status][4]][5]
[![downloads][8]][9] [![js-standard-style][10]][11]

Smol event based component library. Syntactic sugar around [nanocomponent][nc].
Adds logging through [nanologger](https://github.com/yoshuawuyts/nanologger).

## Usage
```js
var microcomponent = require('microcomponent')
var html = require('bel')

var component = createComponent()
document.body.appendChild(component.render())

function createComponent () {
  var component = microcomponent({
    props: {
      text: null
    }
  })
  component.on('render', render)
  component.on('update', update)
  component.on('load', load)
  component.on('unload', unload)
  return component

  function render () {
    return html`<h1>${this.props.text}</h1>`
  }

  function update (props) {
    return props.text !== this.props.text
  }

  function load () {
    console.log('mounted on DOM')
  }

  function unload () {
    console.log('removed from DOM')
  }
}
```

## API
### `component = Component([{ name, props, state, pure }])`
Create a new Microcomponent instance. Takes a name string that's used for
logging data. Logging is logged on log level `'debug'`. You can set the log
level through `localstorage.logLevel = 'debug|info|warn|error|fatal'`. Also
takes objects that will be initialized as `this.state` and `this.props`. For
reference, a `this.oldProps` will always contain the state of the previous
render iteration.

Set `pure = true` to activate the default `on('update')` handler, which will
shallow diff `props` with `this.props`.

### `component.on(eventname, handler)`
Register a new handler for an eventname. Can register any custom event,
built-in lifecycle events are:
- `render`: (required) create a new DOMNode. If there's already been an DOMNode
  rendered it'll be diffed instead. Must always return an DOMNode of the same
  type.
- `update`: (required) determine if `update` should be called
- `load`: called when the element is mounted on the DOM
- `unload`: called when the element is removed from the DOM

### `component.emit(eventname, [props])`
Trigger a handler on the component.

### `DOMNode = component.render([props])`
Render an element.

## See Also
- [yoshuawuyts/nanocomponent][nc]

## License
[MIT](https://tldrlegal.com/license/mit-license)

[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square
[1]: https://nodejs.org/api/documentation.html#documentation_stability_index
[2]: https://img.shields.io/npm/v/microcomponent.svg?style=flat-square
[3]: https://npmjs.org/package/microcomponent
[4]: https://img.shields.io/travis/yoshuawuyts/microcomponent/master.svg?style=flat-square
[5]: https://travis-ci.org/yoshuawuyts/microcomponent
[6]: https://img.shields.io/codecov/c/github/yoshuawuyts/microcomponent/master.svg?style=flat-square
[7]: https://codecov.io/github/yoshuawuyts/microcomponent
[8]: http://img.shields.io/npm/dm/microcomponent.svg?style=flat-square
[9]: https://npmjs.org/package/microcomponent
[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[11]: https://github.com/feross/standard
[nc]: https://github.com/yoshuawuyts/nanocomponent

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