{"id":21547265,"url":"https://github.com/the-road-to-learn-react/use-state-with-callback","last_synced_at":"2025-04-13T02:18:12.888Z","repository":{"id":34980604,"uuid":"189572328","full_name":"the-road-to-learn-react/use-state-with-callback","owner":"the-road-to-learn-react","description":"Custom hook to include a callback function for useState.","archived":false,"fork":false,"pushed_at":"2022-04-14T21:07:50.000Z","size":1720,"stargazers_count":276,"open_issues_count":7,"forks_count":36,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-13T02:17:22.920Z","etag":null,"topics":["callback","react","react-hook","react-hooks","react-use-state-callback","reactjs"],"latest_commit_sha":null,"homepage":"https://www.robinwieruch.de","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/the-road-to-learn-react.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"rwieruch","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2019-05-31T10:02:22.000Z","updated_at":"2025-04-03T09:57:21.000Z","dependencies_parsed_at":"2022-09-09T21:30:59.591Z","dependency_job_id":null,"html_url":"https://github.com/the-road-to-learn-react/use-state-with-callback","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/the-road-to-learn-react%2Fuse-state-with-callback","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/the-road-to-learn-react%2Fuse-state-with-callback/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/the-road-to-learn-react%2Fuse-state-with-callback/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/the-road-to-learn-react%2Fuse-state-with-callback/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/the-road-to-learn-react","download_url":"https://codeload.github.com/the-road-to-learn-react/use-state-with-callback/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248654104,"owners_count":21140237,"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":["callback","react","react-hook","react-hooks","react-use-state-callback","reactjs"],"created_at":"2024-11-24T06:14:39.525Z","updated_at":"2025-04-13T02:18:12.848Z","avatar_url":"https://github.com/the-road-to-learn-react.png","language":"JavaScript","funding_links":["https://github.com/sponsors/rwieruch"],"categories":[],"sub_categories":[],"readme":"# useStateWithCallback React Hook\n\n[![Build Status](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback.svg?branch=master)](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback) [![Greenkeeper badge](https://badges.greenkeeper.io/the-road-to-learn-react/use-state-with-callback.svg)](https://greenkeeper.io/) ![NPM](https://img.shields.io/npm/l/use-state-with-callback.svg)\n\nCustom hook to include a callback function for useState which was previously available for setState in class components. [Read more about it here.](https://www.robinwieruch.de/react-usestate-callback/)\n\n## Installation\n\n`npm install use-state-with-callback`\n\n## Usage\n\n**useStateWithCallback:**\n\n```jsx\nimport * as React from 'react';\n\nimport useStateWithCallback from 'use-state-with-callback';\n\n// Note: cannot be used on the server-side (e.g. Next.js)\n// import { useStateWithCallbackInstant } from 'use-state-with-callback';\n\nconst App = () =\u003e {\n  const [count, setCount] = useStateWithCallback(0, currentCount =\u003e {\n    console.log('render, then callback.');\n    console.log('otherwise use useStateWithCallbackInstant()');\n\n    if (currentCount \u003e 1) {\n      console.log('Threshold of over 1 reached.');\n    } else {\n      console.log('No threshold reached.');\n    }\n  });\n\n  // const [count, setCount] = useStateWithCallbackInstant(0, currentCount =\u003e {\n  //   console.log('render with instant callback.');\n\n  //   if (currentCount \u003e 1) {\n  //     console.log('Threshold of over 1 reached.');\n  //   } else {\n  //     console.log('No threshold reached.');\n  //   }\n  // });\n\n  const handleClick = () =\u003e {\n    setCount(count + 1);\n  };\n\n  return (\n    \u003cdiv\u003e\n      {count}\n\n      \u003cbutton type=\"button\" onClick={handleClick}\u003e\n        Increase\n      \u003c/button\u003e\n    \u003c/div\u003e\n  );\n};\n\nexport default App;\n```\n\n**useStateWithCallbackLazy:**\n\n```jsx\nimport * as React from 'react';\nimport { useStateWithCallbackLazy } from 'use-state-with-callback';\n\nconst App = () =\u003e {\n  const [count, setCount] = useStateWithCallbackLazy(0);\n\n  const handleClick = () =\u003e {\n    setCount(count + 1, (currentCount) =\u003e {\n      if (currentCount \u003e 1) {\n        console.log('Threshold of over 1 reached.');\n      } else {\n        console.log('No threshold reached.');\n      }\n    });\n  };\n\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003e{count}\u003c/p\u003e\n\n      \u003cbutton type=\"button\" onClick={handleClick}\u003e\n        Increase\n      \u003c/button\u003e\n    \u003c/div\u003e\n  );\n};\n\nexport default App;\n```\n\n## Pitfalls\n\n* When a state update is called with the current value and optimized away, the callback is never called.\n* `useStateWithCallbackLazy` calls the callback with the scope that existed before update, while this.setState callback can access the updated this.state and `get something()` computed values. This can't be fixed, but it's a problem for people who expect a drop-in replacement.\n* When `useStateWithCallbackLazy` state is updated several times with batching (e.g. in an event handler), only the last update calls the callback.\n\n## Contribute\n\n- `git clone git@github.com:the-road-to-learn-react/use-state-with-callback.git`\n- `cd use-state-with-callback`\n- `npm install`\n- `npm run test`\n\n### More\n\n- [Publishing a Node Package to NPM](https://www.robinwieruch.de/publish-npm-package-node/)\n- [Node.js Testing Setup](https://www.robinwieruch.de/node-js-testing-mocha-chai/)\n- [React Testing Setup](https://www.robinwieruch.de/react-testing-tutorial/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthe-road-to-learn-react%2Fuse-state-with-callback","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthe-road-to-learn-react%2Fuse-state-with-callback","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthe-road-to-learn-react%2Fuse-state-with-callback/lists"}