{"id":17859923,"url":"https://github.com/distolma/immer-svelte","last_synced_at":"2025-03-20T19:31:17.773Z","repository":{"id":38785345,"uuid":"250201598","full_name":"distolma/immer-svelte","owner":"distolma","description":null,"archived":false,"fork":false,"pushed_at":"2023-01-05T17:16:18.000Z","size":481,"stargazers_count":4,"open_issues_count":18,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-16T14:54:11.846Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/distolma.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2020-03-26T08:28:55.000Z","updated_at":"2020-10-10T11:47:04.000Z","dependencies_parsed_at":"2023-02-04T08:15:59.200Z","dependency_job_id":null,"html_url":"https://github.com/distolma/immer-svelte","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distolma%2Fimmer-svelte","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distolma%2Fimmer-svelte/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distolma%2Fimmer-svelte/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distolma%2Fimmer-svelte/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/distolma","download_url":"https://codeload.github.com/distolma/immer-svelte/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244574777,"owners_count":20474821,"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-10-28T07:04:11.265Z","updated_at":"2025-03-20T19:31:17.103Z","avatar_url":"https://github.com/distolma.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# immer-svelte\n\n[![npm version](https://badge.fury.io/js/immer-svelte.svg)](https://www.npmjs.com/package/immer-svelte)\n[![Build Status](https://travis-ci.com/distolma/immer-svelte.svg?branch=master)](https://travis-ci.com/distolma/immer-svelte)\n\n## Installation\n\n```sh\nnpm i -S immer immer-svelte\n```\nor\n```sh\nyarn add immer immer-svelte\n```\n\n## API\n\n#### useImmer\n\nThe function returns a tuple, the first value of the tuple is the current state, the second is the updater function,\nwhich accepts an [immer producer function](https://github.com/mweststrate/immer#api), in which the `draft` can be mutated freely, until the producer ends and the changes will be made immutable and become the next state.\n\n```html\n\u003cscript\u003e\n  import { useImmer } from \"immer-svelte\";\n\n  const [state, updateState] = useImmer({ count: 0 });\n\n  function increase() {\n    updateState(draft =\u003e {\n      draft.count++;\n    });\n  }\n\u003c/script\u003e\n\n\u003cdiv\u003eCount: {$state.count}\u003c/div\u003e\n\u003cbutton on:click={increase}\u003eIncrease\u003c/button\u003e\n```\n\n#### useImmerReducer\n\n```html\n\u003cscript\u003e\n  import { useImmerReducer } from \"immer-svelte\";\n\n  const initialState = { count: 0 };\n\n  function reducer(draft, action) {\n    switch (action.type) {\n      case \"reset\":\n        return initialState;\n      case \"increment\":\n        return void draft.count++;\n      case \"decrement\":\n        return void draft.count--;\n    }\n  }\n\n  const [state, dispatch] = useImmerReducer(reducer, initialState);\n\u003c/script\u003e\n\n\u003cdiv\u003e\n  Count: {$state.count}\n  \u003cbutton on:click={() =\u003e dispatch({ type: \"reset\" })}\u003eReset\u003c/button\u003e\n  \u003cbutton on:click={() =\u003e dispatch({ type: \"increment\" })}\u003e+\u003c/button\u003e\n  \u003cbutton on:click={() =\u003e dispatch({ type: \"decrement\" })}\u003e-\u003c/button\u003e\n\u003c/div\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdistolma%2Fimmer-svelte","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdistolma%2Fimmer-svelte","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdistolma%2Fimmer-svelte/lists"}