{"id":13726572,"url":"https://github.com/bloodyowl/rescript-react-update","last_synced_at":"2025-04-09T10:10:38.195Z","repository":{"id":42520121,"uuid":"182125273","full_name":"bloodyowl/rescript-react-update","owner":"bloodyowl","description":"useReducer with updates and side effects!","archived":false,"fork":false,"pushed_at":"2024-07-22T17:01:58.000Z","size":146,"stargazers_count":96,"open_issues_count":4,"forks_count":10,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-10-30T08:34:16.094Z","etag":null,"topics":["react","reason-react","reasonml","side-effects","update"],"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":"2019-04-18T16:53:57.000Z","updated_at":"2024-06-10T09:34:44.000Z","dependencies_parsed_at":"2024-02-04T18:09:14.258Z","dependency_job_id":"d2201e1b-2655-4d35-9ba6-da8488df1165","html_url":"https://github.com/bloodyowl/rescript-react-update","commit_stats":{"total_commits":48,"total_committers":6,"mean_commits":8.0,"dds":0.625,"last_synced_commit":"7a7de8065c5f33708ad015dc6a54240768c08726"},"previous_names":["bloodyowl/reason-react-update"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bloodyowl%2Frescript-react-update","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bloodyowl%2Frescript-react-update/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bloodyowl%2Frescript-react-update/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bloodyowl%2Frescript-react-update/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bloodyowl","download_url":"https://codeload.github.com/bloodyowl/rescript-react-update/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248018061,"owners_count":21034048,"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":["react","reason-react","reasonml","side-effects","update"],"created_at":"2024-08-03T01:03:13.286Z","updated_at":"2025-04-09T10:10:38.174Z","avatar_url":"https://github.com/bloodyowl.png","language":"ReScript","funding_links":["https://github.com/sponsors/bloodyowl"],"categories":["ReScript"],"sub_categories":[],"readme":"# rescript-react-update\n\n\u003e useReducer with updates and side effects!\n\n## Installation\n\n```console\n$ yarn add rescript-react-update\n```\n\nor\n\n```console\n$ npm install --save rescript-react-update\n```\n\nThen add `rescript-react-update` to your `bsconfig.json` `bs-dependencies` field.\n\n## ReactUpdate.useReducer\n\n```reason\ntype state = int;\n\ntype action =\n  | Increment\n  | Decrement;\n\n[@react.component]\nlet make = () =\u003e {\n  let (state, send) =\n    ReactUpdate.useReducer((state, action) =\u003e\n      switch (action) {\n      | Increment =\u003e Update(state + 1)\n      | Decrement =\u003e Update(state - 1)\n      },\n      0\n    );\n  \u003cdiv\u003e\n    {state-\u003eReact.int}\n    \u003cbutton onClick={_ =\u003e send(Decrement)}\u003e {\"-\"-\u003eReact.string} \u003c/button\u003e\n    \u003cbutton onClick={_ =\u003e send(Increment)}\u003e {\"+\"-\u003eReact.string} \u003c/button\u003e\n  \u003c/div\u003e;\n};\n```\n\n### Lazy initialisation\n\n## ReactUpdate.useReducerWithMapState\n\nIf you'd rather initialize state lazily (if there's some computation you don't want executed at every render for instance), use `useReducerWithMapState` where the first argument is a function taking `unit` and returning the initial state.\n\n```reason\ntype state = int;\n\ntype action =\n  | Increment\n  | Decrement;\n\n[@react.component]\nlet make = () =\u003e {\n  let (state, send) =\n    ReactUpdate.useReducerWithMapState(\n      (state, action) =\u003e\n        switch (action) {\n        | Increment =\u003e Update(state + 1)\n        | Decrement =\u003e Update(state + 1)\n        },\n        () =\u003e 0\n    );\n  \u003cdiv\u003e\n    {state-\u003eReact.int}\n    \u003cbutton onClick={_ =\u003e send(Decrement)}\u003e {\"-\"-\u003eReact.string} \u003c/button\u003e\n    \u003cbutton onClick={_ =\u003e send(Increment)}\u003e {\"+\"-\u003eReact.string} \u003c/button\u003e\n  \u003c/div\u003e;\n};\n```\n\n### Cancelling a side effect\n\nThe callback you pass to `SideEffects` \u0026 `UpdateWithSideEffect` returns an `option(unit =\u003e unit)`, which is the cancellation function.\n\n```reason\n// doesn't cancel\nSideEffects(({send}) =\u003e {\n  Js.log(1);\n  None\n});\n// cancels\nSideEffects(({send}) =\u003e {\n  let request = Request.make();\n  request-\u003eFuture.get(payload =\u003e send(Receive(payload)))\n  Some(() =\u003e {\n    Request.cancel(request)\n  })\n});\n```\n\nIf you want to copy/paste old reducers that don't support cancellation, you can use `ReactUpdateLegacy` instead in place of `ReactUpdate`. Its `SideEffects` and `UpdateWithSideEffects` functions accept functions that return `unit`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbloodyowl%2Frescript-react-update","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbloodyowl%2Frescript-react-update","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbloodyowl%2Frescript-react-update/lists"}