{"id":13726580,"url":"https://github.com/bloodyowl/rescript-recoil","last_synced_at":"2025-03-15T14:30:32.932Z","repository":{"id":45789138,"uuid":"264625851","full_name":"bloodyowl/rescript-recoil","owner":"bloodyowl","description":"Zero-cost bindings to Facebook's Recoil library","archived":false,"fork":false,"pushed_at":"2024-03-25T19:16:02.000Z","size":296,"stargazers_count":154,"open_issues_count":5,"forks_count":17,"subscribers_count":7,"default_branch":"main","last_synced_at":"2024-04-14T12:00:28.106Z","etag":null,"topics":["bucklescript","react","reason-react","reasonml","recoil"],"latest_commit_sha":null,"homepage":null,"language":"ReScript","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/bloodyowl.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":null,"funding":".github/FUNDING.yml","license":"MIT-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},"funding":{"github":["bloodyowl"]}},"created_at":"2020-05-17T09:23:57.000Z","updated_at":"2024-05-10T23:37:12.679Z","dependencies_parsed_at":"2024-05-10T23:47:13.343Z","dependency_job_id":null,"html_url":"https://github.com/bloodyowl/rescript-recoil","commit_stats":null,"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bloodyowl%2Frescript-recoil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bloodyowl%2Frescript-recoil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bloodyowl%2Frescript-recoil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bloodyowl%2Frescript-recoil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bloodyowl","download_url":"https://codeload.github.com/bloodyowl/rescript-recoil/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243112144,"owners_count":20238185,"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":["bucklescript","react","reason-react","reasonml","recoil"],"created_at":"2024-08-03T01:03:13.528Z","updated_at":"2025-03-15T14:30:32.393Z","avatar_url":"https://github.com/bloodyowl.png","language":"ReScript","funding_links":["https://github.com/sponsors/bloodyowl"],"categories":["ReScript"],"sub_categories":[],"readme":"# rescript-recoil\n\n![Node.js CI](https://github.com/bloodyowl/rescript-recoil/workflows/Node.js%20CI/badge.svg)\n\n\u003e Zero-cost bindings to Facebook's [Recoil](https://recoiljs.org) library\n\n⚠️ These bindings are still in experimental stages, use with caution\n\n## Installation\n\nRun the following command:\n\n```console\n$ yarn add recoil rescript-recoil\n```\n\nThen add `rescript-recoil` to your `bsconfig.json`'s dependencies:\n\n```diff\n {\n   \"bs-dependencies\": [\n+    \"rescript-recoil\"\n   ]\n }\n```\n\n## Usage\n\n### Atom\n\n```rescript\nlet textState = Recoil.atom({\n  key: \"textState\",\n  default: \"\",\n})\n```\n\n### Selector\n\nA nice feature the OCaml type-system enables is the ability to differenciate Recoil values between the ones that can **only read state** with the ones that can **write state**. This way, you **can't** use hooks with write capabilities with a read-only value.\n\n#### With read only capabilities\n\n```rescript\nlet textStateSize = Recoil.selector({\n  key: \"textStateSize\",\n  get: ({get}) =\u003e {\n    let textState = get(textState)\n    Js.String.length(textState)\n  },\n})\n```\n\n#### With write capabilities\n\n```rescript\nlet textStateSize = Recoil.selectorWithWrite({\n  key: \"textStateSize\",\n  get: ({get}) =\u003e {\n    let textState = get(textState);\n    Js.String.length(textState);\n  },\n  set: ({set}, newSize) =\u003e {\n    let currentTextState = get(textState);\n    set(textState, currentTextState-\u003eJs.String.slice(~from=0, ~to_=newSize));\n  }\n});\n```\n\n#### Async\n\n```rescript\nlet user = Recoil.asyncSelector({\n  key: \"user\",\n  get: ({get}) =\u003e {\n    fetchUser(get(currentUserId))\n  },\n})\n```\n\n### Hooks\n\n#### `useRecoilState`\n\n```rescript\nlet (state, setState) = Recoil.useRecoilState(textState);\n\nstate // read\nsetState(textState =\u003e newTextState) // write\n```\n\n#### `useRecoilValue`\n\n```rescript\nlet state = Recoil.useRecoilValue(textState)\n\nstate // read\n```\n\n#### `useSetRecoilState`\n\n```rescript\nlet setState = Recoil.useSetRecoilState(textState)\n\nsetState(textState =\u003e newTextState) // write\n```\n\n#### `useResetRecoilState`\n\n```rescript\nlet reset = Recoil.useResetRecoilState(textState)\n\nreset() // write\n```\n\n#### `useRecoilCallback`\n\n- Dependency free (reevaluates the callback at every render): `useRecoilCallback(...)`\n- Zero-dependency (never reevaluates the callback): `useRecoilCallback0(...)`\n- One-dependency (reevaluates when dep changes): `useRecoilCallback1(..., [|dep|])`\n- Multiple-dependencies: `useRecoilCallback2(..., (dep1, dep2))` (goes from 2 to 5)\n\n```rescript\nlet onClick = Recoil.useRecoilCallback(({snapshot: {getPromise}}, event) =\u003e {\n  let _ = getPromise(myAtom)\n    |\u003e Js.Promise.then_(value =\u003e {\n      Js.log(value)\n      Js.Promise.resolve()\n    })\n})\n\n\u003cbutton onClick={onClick}\u003e\n  {\"Click me\"-\u003eReact.string}\n\u003c/button\u003e\n```\n\n#### `useRecoilValueLoadable`\n\n```rescript\nlet loadable = Recoil.useRecoilValueLoadable(textState)\n\nJs.log(loadable-\u003eRecoil.Loadable.state)\nJs.log(loadable-\u003eRecoil.Loadable.getValue)\n```\n\n## Examples\n\nThe [Recoil Basic Tutorial](https://recoiljs.org/docs/basic-tutorial/intro) has been made in ReasonReact: [check the source](./examples/TodoList.re)!\n\nYou can run it using:\n\n```console\n$ yarn examples\n```\n\nand going to [http://localhost:8000/TodoList.html](http://localhost:8000/TodoList.html)\n\n### Memoization\n\n```rescript\ntype t = {\n  id: string,\n  value: string,\n  isCompleted: bool,\n}\n\n// For atoms\nlet todoItemFamily = Recoil.atomFamily({\n  key: \"todo\",\n  default: param =\u003e {\n    id: param,\n    value: \"\",\n    isCompleted: false,\n  },\n})\n\n// For selectors\nlet todoItemLengthFamily = Recoil.selectorFamily({\n  key: \"todo\",\n  // The `Fn` wrapper is needed here so that BuckleScript\n  // outputs the correct value\n  get: param =\u003e Fn(({get}) =\u003e {\n    get(todoItemFamily(param)).value-\u003eJs.String2.length\n  }),\n})\n```\n\nAnd use it within a React component:\n\n```rescript\n[@react.component]\nlet make = (~todoId) =\u003e {\n  let (todo, setTodo) = Recoil.useRecoilState(todoItemFamily(id))\n\n  // ...\n  \u003c\u003e\n    {todo.value-\u003eReact.string}\n  \u003c/\u003e\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbloodyowl%2Frescript-recoil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbloodyowl%2Frescript-recoil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbloodyowl%2Frescript-recoil/lists"}