{"id":16124398,"url":"https://github.com/jongacnik/component-box","last_synced_at":"2025-03-18T12:31:41.549Z","repository":{"id":68335692,"uuid":"103699228","full_name":"jongacnik/component-box","owner":"jongacnik","description":"A little component cacher 📦","archived":false,"fork":false,"pushed_at":"2019-01-25T19:35:03.000Z","size":13,"stargazers_count":25,"open_issues_count":1,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-28T09:21:12.408Z","etag":null,"topics":["cache","choo","nanocomponent"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jongacnik.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-15T20:38:38.000Z","updated_at":"2019-03-25T00:27:03.000Z","dependencies_parsed_at":"2023-06-25T23:59:36.678Z","dependency_job_id":null,"html_url":"https://github.com/jongacnik/component-box","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jongacnik%2Fcomponent-box","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jongacnik%2Fcomponent-box/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jongacnik%2Fcomponent-box/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jongacnik%2Fcomponent-box/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jongacnik","download_url":"https://codeload.github.com/jongacnik/component-box/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243926065,"owners_count":20369911,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","choo","nanocomponent"],"created_at":"2024-10-09T21:21:14.673Z","updated_at":"2025-03-18T12:31:41.542Z","avatar_url":"https://github.com/jongacnik.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003ecomponent-box 📦\u003c/h1\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://nodejs.org/api/documentation.html#documentation_stability_index\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square\" alt=\"Stability\" /\u003e\n  \u003c/a\u003e\n   \u003ca href=\"https://www.npmjs.com/package/component-box\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/v/component-box.svg?style=flat-square\" alt=\"NPM version\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://travis-ci.org/jongacnik/component-box\"\u003e\n    \u003cimg src=\"https://img.shields.io/travis/jongacnik/component-box/master.svg?style=flat-square\"\n      alt=\"Build Status\" /\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n\u003cbr /\u003e\n\nA little component cacher for things like [nanocomponent](https://github.com/choojs/nanocomponent)\n\n## Usage\n\n```js\nvar Nanocomponent = require('nanocomponent')\nvar html = require('bel')\nvar c = require('component-box')\n\n// component\nclass MyComponent extends Nanocomponent {\n  createElement (text) {\n    return html`\u003cdiv\u003e${text}\u003c/div\u003e`\n  }\n}\n\n// function which returns component instance\nfunction createMyComponent () {\n  return new MyComponent()\n}\n\nc.use({\n  mycomponent: createMyComponent\n})\n\n// return and render new `mycomponent`\nc('mycomponent').render()\n```\n\nMeanwhile in another file...\n\n```js\nvar c = require('component-box')\n\n// return and render cached `mycomponent`\nc('mycomponent').render()\n```\n\n## API\n\n### `component = c(name, key)`\n\nReturn a component. `key` is optional. Works as follows:\n\n```js\n// return new `mycomponent` and cache as `mycomponent`\nc('mycomponent')\n\n// return cached `mycomponent`\nc('mycomponent')\n\n// return new `mycomponent` and cache as `boop`\nc('mycomponent', 'boop')\n\n// return cached `mycomponent` with key `boop`\nc('mycomponent', 'boop')\n\n// always return new `mycomponent` (never cache)\nc('mycomponent', false)\n```\n\n### `c.use([components])`\n\nAdd components to the store. Object values expect Functions which return a component.\n\n```js\nvar c = require('component-box')\n\nc.use({\n  beep: beepComponent,\n  boop: boopComponent\n})\n\nc('beep').render()\nc('boop').render()\n```\n\n These are shared throughout your app, so in another file you don't need to re-`.use`:\n\n```js\nvar c = require('component-box')\n\nc('beep').render()\nc('boop').render()\n```\n\n### `c.delete(key)`\n\nRemove component from the cache\n\n```js\n// return new `mycomponent` and cache as `mycomponent`\nc('mycomponent')\n\n// remove `mycomponent` from cache\nc.delete('mycomponent')\n\n// return new `mycomponent` and cache as `mycomponent`\nc('mycomponent')\n```\n\n### `c.cache(cache)`\n\nUse a custom cache implementation. Expects an object with `.get`, `.set`, and `.remove` methods.\n\n```js\nvar c = require('component-box')\n\nc.cache(require('lru')(2)) // only cache 2 instances, using lru eviction\n\nc.use({\n  post: postComponent\n})\n\nc('post', 'slug1').render()\nc('post', 'slug2').render()\nc('post', 'slug3').render() // evict 'post-slug1' and return new 'post-slug3' instance\n```\n\n## Alternative Pattern\n\nYou could also choose to only return the `.render` method from nanocomponent, allowing for a slightly more concise syntax:\n\n```js\n// function which creates component instance and returns render method\nfunction createMyComponent () {\n  var component = new MyComponent()\n  return function () {\n    return component.render(...arguments)\n  }\n}\n```\n\n```js\n// directly calls nanocomponent .render\nc('mycomponent')()\n```\n\n## Real World\n\nThis 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. \n\nIn this example we are using post slugs as component keys:\n\n```js\nvar html = require('bel')\nvar c = require('component-box')\n\nc.use({\n  post: require('my-post-component')\n})\n\nvar postsData = [\n  { title: 'Beep', slug: 'beep' },\n  { title: 'Boop', slug: 'boop' },\n  { title: 'Blap', slug: 'blap' }\n]\n\nfunction view () {\n  var posts = postsData.map(function (post) {\n    return c('post', post.slug).render(post)\n  })\n\n  return html`\u003cdiv\u003e${posts}\u003c/div\u003e`\n}\n\n```\n\n## More Examples\n\n### [fun-component](https://github.com/tornqvist/fun-component)\n\n```js\nvar component = require('fun-component')\nvar html = require('bel')\nvar c = require('component-box')\n\n// function which returns a component\nfunction mycomponent () {\n  return component(function (props) {\n    return html`\u003cdiv\u003eHello!\u003cdiv\u003e`\n  })\n}\n\nc.use({\n  mycomponent: mycomponent\n})\n\n// return new `mycomponent`\nc('mycomponent')()\n```\n\n## See Also\n\n- [choojs/nanocomponent](https://github.com/choojs/nanocomponent)\n- [tornqvist/fun-component](https://github.com/tornqvist/fun-component)\n\n## License\n\n[MIT](https://tldrlegal.com/license/mit-license)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjongacnik%2Fcomponent-box","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjongacnik%2Fcomponent-box","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjongacnik%2Fcomponent-box/lists"}