{"id":25975790,"url":"https://github.com/mac-r/unstated-props","last_synced_at":"2025-03-05T03:24:10.229Z","repository":{"id":57386885,"uuid":"171865478","full_name":"mac-r/unstated-props","owner":"mac-r","description":"Provides access to your Unstated containers from component props.","archived":false,"fork":false,"pushed_at":"2019-02-25T16:19:10.000Z","size":36,"stargazers_count":22,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-18T03:06:03.742Z","etag":null,"topics":["higher-order-component","npm-package","preact","react","state-management","unstated"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/mac-r.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}},"created_at":"2019-02-21T12:12:05.000Z","updated_at":"2023-09-03T23:16:03.000Z","dependencies_parsed_at":"2022-09-26T16:50:43.755Z","dependency_job_id":null,"html_url":"https://github.com/mac-r/unstated-props","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/mac-r%2Funstated-props","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mac-r%2Funstated-props/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mac-r%2Funstated-props/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mac-r%2Funstated-props/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mac-r","download_url":"https://codeload.github.com/mac-r/unstated-props/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241801207,"owners_count":20022384,"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":["higher-order-component","npm-package","preact","react","state-management","unstated"],"created_at":"2025-03-05T03:24:09.292Z","updated_at":"2025-03-05T03:24:10.221Z","avatar_url":"https://github.com/mac-r.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## unstated-props\n\n[![NPM](http://img.shields.io/npm/v/unstated-props.svg?style=flat)](https://www.npmjs.org/package/unstated-props)\n[![Build Status](https://travis-ci.org/mac-r/unstated-props.svg?branch=master)](https://travis-ci.org/mac-r/unstated-props)\n\n\u003e A higher-order component that enables access to your unstated containers from\u003cbr/\u003e component props. No need to set up a `Provider` or `Subscribe` wrappers.\n\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"800\" src='https://cdn.blinkloader.com/express/rF9GczNCw0srKYaWh0NlluCOM/unstated-props.png' alt=\"unstated-props usage demo\" /\u003e\n\u003c/p\u003e\n\n### 👋 Hi there!\n\n**unstated-props** is an abstraction layer on\ntop of **unstated** by **@jamiebuilds**:\u003cbr/\u003e\nhttps://github.com/jamiebuilds/unstated\n\nThis is a higher-order component that enables access to your unstated containers from component props. No need to set up a `Provider` or clunky `Subscribe` wrappers. This one is a humble attempt to make a masterpiece like `unstated` even better. ✨\n\n🐣 **\u003c 990 bytes** when used directly in the browser \u003cbr/\u003e\n⚛️ **React / preact** compatible (thanks to \u003ca href=\"https://github.com/developit/preact-compat\"\u003epreact-compat\u003c/a\u003e)\n\n### Installation\n\nTo install the stable version:\n\n```\nnpm install --save unstated-props\n```\n\nThis assumes you are using `npm` as your package manager.\n\n### Usage\n\nSince `unstated-props` is an extension on top of `unstated`, we create `Containers` as we would normally do.\nIt's recommended to put all of your containers into a single folder. Your application\nstructure would look similar to the following:\n\n```\nsrc/\n  app.jsx\n  components/\n    Button.jsx\n    Menu.jsx\n    ...\n  containers/\n    PlaylistContainer.js\n    SettingsContainer.js\n    ...\n    index.js\n```\n\n#### Step 1. Put unstated containers into \"containers\" folder\n\nYour containers are the original `unstated` containers:\n\n```js\n// containers/PlaylistContainer.js\n// A good old unstated Container, simple and easy to read.\n\nimport { Container } from 'unstated';\n\nclass PlaylistContainer extends Container {\n  state = { shuffleMode: false };\n\n  shuffle = async () =\u003e {\n    await this.setState({ shuffleMode: true });\n    console.log(this.state.shuffleMode); // true\n  };\n}\n\nexport default PlaylistContainer;\n```\n\n#### Step 2. Create index.js in the \"containers\" folder\n\nThen you need to create the `index.js` file in your `containers/` folder.\nThis is a place where you import `unstated-props`. You can organize it\nin a way similar to this:\n\n```js\n// containers/index.js\n// The major trick happens here.\n\nimport { connect } from 'unstated-props';\nimport PlaylistContainer from './PlaylistContainer';\nimport SettingsContainer from './SettingsContainer';\n\n// \"playlist\" and \"settings\" will become available\n// through \"this.props.containers\" in your components\nexport default connect({\n  playlist: PlaylistContainer,\n  settings: SettingsContainer\n});\n```\n\n#### Step 3. Set up the root component\n\nLet's say that our demo app has the core component named `App`.\nThis is the root of the whole thing.\n\nWe wrap it with a root flag:\n\n```js\n// Root component \"src/app.jsx\".\n// TLDR: don't forget about the root flag! 😉\n\nimport React from 'react';\nimport withContainers from './containers';\n\nconst App = () =\u003e (\n  \u003cdiv\u003e\n    Many different things will be added here.\n  \u003c/div\u003e\n)\n\nexport default withContainers(App, { root: true })\n```\n\n#### Step 4. Use containers where needed\n\nNow we can access containers from our components. It's really easy and\nthe end result looks great:\n\n```js\n// components/Menu.jsx\n\nimport React from 'react';\nimport withContainers from '../containers';\n\nconst Menu = ({ containers: { playlist }}) =\u003e (\n  \u003cbutton onClick={()=\u003e { playlist.shuffle() }}\u003e\n    Shuffle mode: {playlist.state.shuffleMode}\n  \u003c/button\u003e\n)\n\nexport default withContainers(Menu);\n```\n\nThat's it. Enjoy! ❤️\n\n**Feel free to star this repo and follow me on Twitter:**\n\n[![Github](https://githubbadges.com/star.svg?user=mac-r\u0026repo=unstated-props\u0026style=flat)](https://www.npmjs.org/package/unstated-props)\n[![Twitter URL](https://img.shields.io/twitter/url/https/twitter.com/makarochkin.svg?style=social\u0026label=Follow%20%40makarochkin)](https://twitter.com/makarochkin)\n\n### Related\n\n\n- [unstated](https://github.com/jamiebuilds/unstated) - State so simple, it goes without saying\n- [unstated-debug](https://github.com/sindresorhus/unstated-debug) - Debug your Unstated containers with ease\n\n----\n\n\nMIT License, 2019. Max Makarochkin\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmac-r%2Funstated-props","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmac-r%2Funstated-props","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmac-r%2Funstated-props/lists"}