Coder Social home page Coder Social logo

microcomponent's Introduction

microcomponent stability

npm version build status downloads js-standard-style

Smol event based component library. Syntactic sugar around nanocomponent. Adds logging through nanologger.

Usage

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

License

MIT

microcomponent's People

Contributors

iiegor avatar juliangruber avatar mfolkeseth avatar yoshuawuyts avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.