{"id":13394449,"url":"https://github.com/jxnblk/refunk","last_synced_at":"2025-03-13T20:31:31.564Z","repository":{"id":57243100,"uuid":"92780098","full_name":"jxnblk/refunk","owner":"jxnblk","description":":headphones: Simple React functional setState","archived":true,"fork":false,"pushed_at":"2018-05-02T00:19:10.000Z","size":293,"stargazers_count":236,"open_issues_count":0,"forks_count":17,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-05-07T20:48:26.866Z","etag":null,"topics":["functional","react","setstate","state"],"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/jxnblk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-29T22:34:09.000Z","updated_at":"2024-05-01T00:36:24.000Z","dependencies_parsed_at":"2022-09-13T03:10:12.350Z","dependency_job_id":null,"html_url":"https://github.com/jxnblk/refunk","commit_stats":null,"previous_names":["jxnblk/funcup"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jxnblk%2Frefunk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jxnblk%2Frefunk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jxnblk%2Frefunk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jxnblk%2Frefunk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jxnblk","download_url":"https://codeload.github.com/jxnblk/refunk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243478117,"owners_count":20297197,"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":["functional","react","setstate","state"],"created_at":"2024-07-30T17:01:20.077Z","updated_at":"2025-03-13T20:31:31.179Z","avatar_url":"https://github.com/jxnblk.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"\n# Refunk 🎧\n\nSimple React functional setState\nwith the new [React context API][context] (requires React v16.3 or later)\n\n\n```sh\nnpm i refunk\n```\n\n## Getting Started\n\n```jsx\nimport React from 'react'\nimport { connect } from 'refunk'\n\n// Create a state provider component\nconst App = connect(props =\u003e (\n  \u003cdiv\u003e\n    \u003ch1\u003ecount: {props.count}\u003c/h1\u003e\n    \u003cControls /\u003e\n  \u003c/div\u003e\n))\n\n// Updaters are functions that return state\nconst dec = state =\u003e ({ count: state.count - 1 })\nconst inc = state =\u003e ({ count: state.count + 1 })\n\n// Connect the Controls component to the App state\nconst Controls = connect(props =\u003e (\n  \u003cdiv\u003e\n    \u003csamp\u003e{props.count}\u003c/samp\u003e\n    \u003cbutton onClick={e =\u003e props.update(dec)}\u003e\n      -\n    \u003c/button\u003e\n    \u003cbutton onClick={e =\u003e props.update(inc)}\u003e\n      +\n    \u003c/button\u003e\n  \u003c/div\u003e\n))\n\nconst initialState = {\n  count: 0\n}\n\n// initialize state with props\nrender(\u003cApp {...initialState} /\u003e)\n```\n\n## Usage\n\nRefunk components initialize state from props and provide an `update` function to their consumers.\nWhen nesting Refunk components, the top-most component will control state for any child Refunk components.\n\nThe `update` function works the same as `setState`, but it's intended to be used with separate [updater functions](#using-updaters),\nthat can be shared across many parts of an application.\n\n### connect\n\nThe `connect` higher-order component creates state based on props for top-level components or connects into a parent Refunk component's state when nested.\nThis allows for the creation of stateful components that can work standalone or listen to a parent's state.\n\n```jsx\nimport React from 'react'\nimport { connect } from 'refunk'\n\nconst App = connect(props =\u003e (\n  \u003cdiv\u003e\n    \u003csamp\u003e{props.count}\u003c/samp\u003e\n  \u003c/div\u003e\n))\n\nApp.defaultProps = {\n  count: 0\n}\n\nexport default App\n```\n\n### Provider\n\nFor lower-level access to React's context API, the Provider component can be used to create a context.\nThe Refunk Provider will convert props to initial state and provide the state and `update` function through context.\n\n```jsx\nimport React from 'react'\nimport { Provider } from 'refunk'\n\nconst App = props =\u003e (\n  \u003cProvider count={0}\u003e\n    \u003cdiv /\u003e\n  \u003c/Provider\u003e\n)\n```\n\n### Consumer\n\nThe context Consumer is also exported for lower-level access to the context API.\n\n```jsx\nimport React from 'react'\nimport { Provider, Consumer } from 'refunk'\n\nconst inc = state =\u003e ({ count: state.count + 1 })\n\nconst App = props =\u003e (\n  \u003cProvider count={0}\u003e\n    \u003cConsumer\u003e\n      {state =\u003e (\n        \u003cReact.Fragment\u003e\n          \u003csamp\u003e{state.count}\u003c/samp\u003e\n          \u003cbutton onClick={e =\u003e state.update(inc)}\u003e+\u003c/button\u003e\n        \u003c/React.Fragment\u003e\n      )}\n    \u003c/Consumer\u003e\n  \u003c/Provider\u003e\n)\n```\n\n### Using Updaters\n\nUpdaters are functions that are passed to the `props.update()` function.\nAn updater function takes `state` as its only argument and returns a new state.\n\n```jsx\n// updaters.js\n// Create an `updaters` module with functions to update the state of the app\nexport const decrement = state =\u003e ({ count: state.count - 1 })\nexport const increment = state =\u003e ({ count: state.count + 1 })\n```\n\n```jsx\n// Counter.js\n// Use the updater functions in the connected Counter component\nimport React from 'react'\nimport { connect } from 'refunk'\nimport { decrement, increment } from './updaters'\n\nconst Counter = props =\u003e (\n  \u003cdiv\u003e\n    \u003csamp\u003eCount: {props.count}\u003c/samp\u003e\n    \u003cbutton onClick={e =\u003e props.update(decrement)}\u003e\n      Decrement\n    \u003c/button\u003e\n    \u003cbutton onClick={e =\u003e props.update(increment)}\u003e\n      Increment\n    \u003c/button\u003e\n  \u003c/div\u003e\n)\n\nexport default connect(Counter)\n```\n\n```jsx\n// App.js\n// Include the Counter component in App\nimport React from 'react'\nimport { connect } from 'refunk'\nimport Counter from './Counter'\n\nconst App = props =\u003e (\n  \u003cdiv\u003e\n    \u003ch1\u003eHello\u003c/h1\u003e\n    \u003cCounter /\u003e\n  \u003c/div\u003e\n)\n\nexport default connect(App)\n```\n\n## Build Your Own\n\nRefunk's [source](src) is only about 50 LOC and relies on built-in React functionality.\nThis library is intended to be used directly as a package and also to serve as an example of some ways to handle state in a React application.\nFeel free to fork or steal ideas from this project, and build your own version.\n\n\n## Concepts\n\nRefunk is meant as a simpler, smaller alternative to other state\nmanagment libraries that makes use of React's built-in component state.\nRefunk uses higher-order components, the new [context API][context], and React component state management along with\n[functional setState][setState]\nto help promote the separation of presentational and container components,\nand to keep state updating logic outside of the components themselves.\n\nThis library also promotes keeping application state in a single location,\nsimilar to other [Flux][flux] libraries and [Redux][redux].\n\n\n### Related\n\n- [microstate](https://github.com/estrattonbailey/microstate)\n- [statty](https://github.com/vesparny/statty)\n- [unistore](https://github.com/developit/unistore)\n- [redux][redux]\n- [unstated](https://github.com/jamiebuilds/unstated)\n\n[context]: https://reactjs.org/docs/context.html\n[setState]: https://facebook.github.io/react/docs/react-component.html#setstate\n[flux]: http://facebook.github.io/flux/\n[redux]: http://redux.js.org/\n\n---\n\n[Made by Jxnblk](http://jxnblk.com) | [MIT License](LICENSE.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjxnblk%2Frefunk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjxnblk%2Frefunk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjxnblk%2Frefunk/lists"}