{"id":13618045,"url":"https://github.com/Aminadav/react-useStateRef","last_synced_at":"2025-04-14T10:30:55.143Z","repository":{"id":38067742,"uuid":"324108783","full_name":"Aminadav/react-useStateRef","owner":"Aminadav","description":"useRef and UseState together!","archived":false,"fork":false,"pushed_at":"2024-05-26T13:15:52.000Z","size":38,"stargazers_count":198,"open_issues_count":3,"forks_count":20,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T11:15:33.183Z","etag":null,"topics":["javascript","react","useref","usestate"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/Aminadav.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,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":"aminadav"}},"created_at":"2020-12-24T08:41:31.000Z","updated_at":"2025-03-17T06:18:06.000Z","dependencies_parsed_at":"2024-04-09T11:40:13.591Z","dependency_job_id":"a799b253-e035-41f3-bd13-4c073c623aae","html_url":"https://github.com/Aminadav/react-useStateRef","commit_stats":null,"previous_names":["aminadav/react-userefstate"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aminadav%2Freact-useStateRef","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aminadav%2Freact-useStateRef/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aminadav%2Freact-useStateRef/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aminadav%2Freact-useStateRef/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Aminadav","download_url":"https://codeload.github.com/Aminadav/react-useStateRef/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248557819,"owners_count":21124167,"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":["javascript","react","useref","usestate"],"created_at":"2024-08-01T20:01:53.249Z","updated_at":"2025-04-14T10:30:54.829Z","avatar_url":"https://github.com/Aminadav.png","language":"TypeScript","readme":"# react-useStateRef\n\n\u003ca href=\"https://www.npmjs.com/package/react-usestateref\"\u003e\u003cimg src=\"https://img.shields.io/static/v1?logo=npm\u0026message=react-useStateRef\"\u003e\u003c/a\u003e\n\nuseRef and UseState together!\n\n## How to use\n\n### Installation\n\n```shell\n$ npm i react-usestateref\n```\n\n```javascript\nimport useState from 'react-usestateref'\nfunction MyComponent(){\n  var [state,setState,ref]=useState(0)\n  // ref.current will always have the latest state  \n}\n```\n\nAs you can see it's 100% backward compatible. You can replace all your `useState` with this import and you will always have the latest state.\n\n## Motivation:\n\nMany StackOverflow questions that people struggling to get the current state\n\n- https://stackoverflow.com/questions/54069253/usestate-set-method-not-reflecting-change-immediately\n- https://stackoverflow.com/questions/60438537/usestate-shows-previous-value-always\n- https://stackoverflow.com/questions/57847594/react-hooks-accessing-up-to-date-state-from-within-a-callback\n- https://stackoverflow.com/questions/55874789/using-react-hooks-why-are-my-event-handlers-firing-with-the-incorrect-state\n\nIn React when functions accessing the state they receive the state from the moment\nthe function defined - not the current state.\n\nSo if the state changed, your functions and effects my use older state.\n\nUsing `useRef`, can solve it because it have always one value. But when you update the Ref it's not re-render.\n\nSee the example code:\n\n```js\nfunction MyComponent(){\n  var [counter,setCounter]=useState(0)\n  function increment(){\n    setCounter(counter+1)\n    alert(counter) // will show 0 since the state not updated yet.\t\t\t\n  }\n  useEffect(()=\u003e{\n    alert(counter) // Whatever is the current state. It always alert 0\n    return ()=\u003e{\n        alert(counter) // On unmount it still alert 0, even if you called increment many times.\n    }\n  },[])\n  return (\n  \u003cdiv\u003e\n    Current number: {counter}\n    \u003cbutton onClick={increment}\u003e\n      Increment\n    \u003c/button\u003e\n  \u003c/div\u003e)\n}\n```\n\nTo solve it I created a new hook `useStateRef`\n\nSee it in action:\n\n```js\nimport useState from 'react-usestateref' // see this line\nfunction MyComponent(){\n  var [counter,setCounter,counterRef]=useState(0)\n  function increment(){\n    setCounter(counter+1)\n    alert(counterRef.current) // will show 1\n  }\n  useEffect(()=\u003e{\n    alert(counterRef.current) // Always show the last value\n    return ()=\u003e{\n        alert(counterRef.current) // // Always show the last value\n    }\n  },[])\n  return (\n  \u003cdiv\u003e\n    Current number: {counter}\n    \u003cbutton onClick={increment}\u003e\n      Increment\n    \u003c/button\u003e\n  \u003c/div\u003e)\n}\n```\n\nIt's fully support the `useState` API, so you can change your `useState` to `useStateRef` and it will not break your app.\n\n## Contribution\n\n- Star \u0026 fork this project.\n- I'm open to your contribution.\n- Better documentation or whatever your like. Just open a PR\n\n## Other innovations by cretors:\n\n- [ביחד ננצח](https://yachad.app)\n- [Webpage Screenshot](https://chromewebstore.google.com/detail/webpage-screenshot-entire/akgpcdalpfphjmfifkmfbpdmgdmeeaeo)","funding_links":["https://github.com/sponsors/aminadav"],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAminadav%2Freact-useStateRef","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAminadav%2Freact-useStateRef","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAminadav%2Freact-useStateRef/lists"}