component-box 📦
A little component cacher for things like nanocomponent
Usage
var Nanocomponent = var html = var c = // component { return html`` } // function which returns component instance { return } c // return and render new `mycomponent`
Meanwhile in another file...
var c = // return and render cached `mycomponent`
API
component = c(name, key)
Return a component. key
is optional. Works as follows:
// return new `mycomponent` and cache as `mycomponent` // return cached `mycomponent` // return new `mycomponent` and cache as `boop` // return cached `mycomponent` with key `boop` // always return new `mycomponent` (never cache)
c.use([components])
Add components to the store. Object values expect Functions which return a component.
var c = c
These are shared throughout your app, so in another file you don't need to re-.use
:
var c =
c.delete(key)
Remove component from the cache
// return new `mycomponent` and cache as `mycomponent` // remove `mycomponent` from cachec // return new `mycomponent` and cache as `mycomponent`
c.cache(cache)
Use a custom cache implementation. Expects an object with .get
, .set
, and .remove
methods.
var c = c // only cache 2 instances, using lru eviction c // evict 'post-slug1' and return new 'post-slug3' instance
Alternative Pattern
You could also choose to only return the .render
method from nanocomponent, allowing for a slightly more concise syntax:
// function which creates component instance and returns render method { var component = return { return component }}
// directly calls nanocomponent .render
Real World
This module is useful when you are creating components on the fly. References to components are saved for you based on keys
, that way on app re-renders it will re-use your components so things like load
and etc are not wonky.
In this example we are using post slugs as component keys:
var html = var c = c var postsData = title: 'Beep' slug: 'beep' title: 'Boop' slug: 'boop' title: 'Blap' slug: 'blap' { var posts = postsData return html``}
More Examples
fun-component
var component = var html = var c = // function which returns a component { return } c // return new `mycomponent`