{"id":13525442,"url":"https://github.com/KwanMan/preact-tiny-atom","last_synced_at":"2025-04-01T05:31:25.149Z","repository":{"id":57329569,"uuid":"103034671","full_name":"KwanMan/preact-tiny-atom","owner":"KwanMan","description":null,"archived":false,"fork":false,"pushed_at":"2017-09-21T09:56:51.000Z","size":8,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-21T11:37:08.400Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KwanMan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-10T14:13:15.000Z","updated_at":"2023-02-15T19:32:28.000Z","dependencies_parsed_at":"2022-09-16T18:00:53.642Z","dependency_job_id":null,"html_url":"https://github.com/KwanMan/preact-tiny-atom","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/KwanMan%2Fpreact-tiny-atom","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KwanMan%2Fpreact-tiny-atom/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KwanMan%2Fpreact-tiny-atom/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KwanMan%2Fpreact-tiny-atom/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KwanMan","download_url":"https://codeload.github.com/KwanMan/preact-tiny-atom/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246591177,"owners_count":20801981,"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":[],"created_at":"2024-08-01T06:01:18.761Z","updated_at":"2025-04-01T05:31:22.131Z","avatar_url":"https://github.com/KwanMan.png","language":"JavaScript","funding_links":[],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"readme":"# preact-tiny-atom\n\nIntegrate [tiny-atom](https://github.com/qubitproducts/tiny-atom) into [preact](https://github.com/developit/preact)\n\n## Usage\n\nSimply wrap your app with the `AtomProvider`:\n\n```js\nconst preact = require('preact')\nconst { AtomProvider } = require('preact-tiny-atom')\nconst createAtom = require('tiny-atom')\n\nconst App = require('./App')\n\nconst atom = createAtom({\n  counts: []\n}, evolve, render)\n\nfunction evolve (get, split, { type, payload }) {\n  if (type === 'addCount') {\n    const prevCounts = get().counts\n    split({\n      counts: prevCounts.concat(payload)\n    })\n  }\n}\n\nfunction render () {\n  const root = (\n    \u003cAtomProvider atom={atom}\u003e\n      \u003cApp /\u003e\n    \u003c/AtomProvider\u003e\n  )\n  preact.render(root, document.body, document.body.firstChild)\n}\n\nrender()\n```\n\nThen for each component where you want to grab something from the state:\n\n```js\n// App.js\nconst preact = require('preact')\nconst { injectAtom } = require('preact-tiny-atom')\n\nconst withAtom = injectAtom({\n  // Pass an array of keys to grab from the top level state\n  grab: ['counts'],\n  // Or an object of key/computer pairs to compute\n  compute: {\n    // Each computer will be given the atom state\n    totalCount: state =\u003e state.counts.reduce((memo, nextCount) =\u003e {\n      return memo + nextCount\n    }, 0)\n    // Or you can pass in an object path string\n    firstCount: 'counts.0'\n  }\n})\n\n// Everything will be passed into the props for your component\nconst App = function App ({ counts, totalCount, firstCount }) {\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003eCounts: [{counts.join(', ')}]\u003c/p\u003e\n      \u003cp\u003eTotal: {totalCount}\u003c/p\u003e\n      \u003cp\u003eFirst Count: {firstCount}\u003c/p\u003e\n    \u003c/div\u003e\n  )\n}\n\nmodule.exports = withAtom(App)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKwanMan%2Fpreact-tiny-atom","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKwanMan%2Fpreact-tiny-atom","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKwanMan%2Fpreact-tiny-atom/lists"}