-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve-Type-Safety-and-State-Access-in-useStateWithDeps-Hook #3027
Merged
huozhi
merged 2 commits into
vercel:main
from
O-BERNARDOFOEGBU:Improve-Type-Safety-and-State-Access-in-useStateWithDeps-Hook
Dec 10, 2024
Merged
Improve-Type-Safety-and-State-Access-in-useStateWithDeps-Hook #3027
huozhi
merged 2 commits into
vercel:main
from
O-BERNARDOFOEGBU:Improve-Type-Safety-and-State-Access-in-useStateWithDeps-Hook
Dec 10, 2024
+26
−16
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
huozhi
force-pushed
the
Improve-Type-Safety-and-State-Access-in-useStateWithDeps-Hook
branch
from
December 9, 2024 23:16
2a55931
to
da30bc1
Compare
revert pnpm-lock
huozhi
force-pushed
the
Improve-Type-Safety-and-State-Access-in-useStateWithDeps-Hook
branch
from
December 9, 2024 23:16
da30bc1
to
0337802
Compare
huozhi
approved these changes
Dec 10, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
alexandresoro
pushed a commit
to alexandresoro/ouca
that referenced
this pull request
Dec 23, 2024
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [swr](https://swr.vercel.app) ([source](https://github.com/vercel/swr)) | dependencies | minor | [`2.2.5` -> `2.3.0`](https://renovatebot.com/diffs/npm/swr/2.2.5/2.3.0) | --- ### Release Notes <details> <summary>vercel/swr (swr)</summary> ### [`v2.3.0`](https://github.com/vercel/swr/releases/tag/v2.3.0) [Compare Source](vercel/swr@v2.2.5...v2.3.0) ##### Feature - Support promises as fallback data by [@​shuding](https://github.com/shuding) in vercel/swr#2891 - Allow to use with React 19 by [@​vladshcherbin](https://github.com/vladshcherbin) and [@​devjiwonchoi](https://github.com/devjiwonchoi) in vercel/swr#3047, vercel/swr#2963 ##### Patches - fix [#​3030](vercel/swr#3030) and run relateive test in edge-runtime by [@​promer94](https://github.com/promer94) in vercel/swr#3036 - fix: Only suspend when using the `fallback` by [@​shuding](https://github.com/shuding) in vercel/swr#3045 - fix type check in tests by [@​huozhi](https://github.com/huozhi) in vercel/swr#3052 - fix: Replace the deprecated 'window' with 'globalThis' for Deno by [@​saul-atomrigs](https://github.com/saul-atomrigs) in vercel/swr#2915 - fix: check if config.fallback is undefined by [@​taku-hatano](https://github.com/taku-hatano) in vercel/swr#2913 - fix(infinte): export SWRInfiniteKeyedMutator type by [@​LeoMcA](https://github.com/LeoMcA) in vercel/swr#2900 - fix: Improve comparison performance by [@​shuding](https://github.com/shuding) in vercel/swr#2973 - Export ScopedMutator type by [@​joshkel](https://github.com/joshkel) in vercel/swr#2937 - Improve-Type-Safety-and-State-Access-in-useStateWithDeps-Hook by [@​O-BERNARDOFOEGBU](https://github.com/O-BERNARDOFOEGBU) in vercel/swr#3027 ##### Misc - chore: bump dev deps and change example react version to latest by [@​huozhi](https://github.com/huozhi) in vercel/swr#2894 - build: fix beta release job by [@​huozhi](https://github.com/huozhi) in vercel/swr#2895 - chore: Improve test coverage by [@​shuding](https://github.com/shuding) in vercel/swr#2903 - chore: simplify test coverage strategy by [@​huozhi](https://github.com/huozhi) in vercel/swr#2909 - build: simplify react-server export and update bundler by [@​huozhi](https://github.com/huozhi) in vercel/swr#2897 - examples: add RSC streaming pre-render with promise fallback example by [@​promer94](https://github.com/promer94) in vercel/swr#2905 - Drop client-only by [@​huozhi](https://github.com/huozhi) in vercel/swr#2910 - Mark package as side-effect free by [@​htunnicliff](https://github.com/htunnicliff) in vercel/swr#2904 - Drop exports module field by [@​huozhi](https://github.com/huozhi) in vercel/swr#2911 - chore: update pkg script watch by [@​unliar](https://github.com/unliar) in vercel/swr#2920 - test: remove console.error times check by [@​promer94](https://github.com/promer94) in vercel/swr#2918 - build: bump bundler for perf by [@​huozhi](https://github.com/huozhi) in vercel/swr#2929 - Fix bundling of client entry chunks by [@​huozhi](https://github.com/huozhi) in vercel/swr#2932 - ci: fix ci error and upgrade action version by [@​promer94](https://github.com/promer94) in vercel/swr#2952 - Add SWRInfiniteMutatorOptions type to export by [@​ludwigbacklund](https://github.com/ludwigbacklund) in vercel/swr#2954 - test: update the revalidate function test for useSWRInfinite by [@​koba04](https://github.com/koba04) in vercel/swr#2955 - chore: upgrade nextjs dev dep for e2e testing by [@​huozhi](https://github.com/huozhi) in vercel/swr#3044 - ci: simplify ci config and bump some deps version by [@​promer94](https://github.com/promer94) in vercel/swr#2770 - chore: reorganize entries by [@​huozhi](https://github.com/huozhi) in vercel/swr#3048 - Bump bundler and reorganize serialize exports by [@​huozhi](https://github.com/huozhi) in vercel/swr#3049 - upgrade use-sync-external-store to support react 19 by [@​huozhi](https://github.com/huozhi) in vercel/swr#3050 #### New Contributors - [@​htunnicliff](https://github.com/htunnicliff) made their first contribution in vercel/swr#2904 - [@​saul-atomrigs](https://github.com/saul-atomrigs) made their first contribution in vercel/swr#2915 - [@​unliar](https://github.com/unliar) made their first contribution in vercel/swr#2920 - [@​ludwigbacklund](https://github.com/ludwigbacklund) made their first contribution in vercel/swr#2954 - [@​taku-hatano](https://github.com/taku-hatano) made their first contribution in vercel/swr#2913 - [@​LeoMcA](https://github.com/LeoMcA) made their first contribution in vercel/swr#2900 - [@​devjiwonchoi](https://github.com/devjiwonchoi) made their first contribution in vercel/swr#2963 - [@​vladshcherbin](https://github.com/vladshcherbin) made their first contribution in vercel/swr#3047 - [@​O-BERNARDOFOEGBU](https://github.com/O-BERNARDOFOEGBU) made their first contribution in vercel/swr#3027 **Full Changelog**: vercel/swr@v2.2.5...v2.3.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS44Mi4zIiwidXBkYXRlZEluVmVyIjoiMzkuODIuMyIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiZGVwZW5kZW5jaWVzIl19--> Reviewed-on: https://git.tristess.app/alexandresoro/ouca/pulls/413 Reviewed-by: Alexandre Soro <[email protected]> Co-authored-by: renovate <[email protected]> Co-committed-by: renovate <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request aims to improve the type safety and reliability of the useStateWithDeps hook in React. Specifically, the changes address potential issues with accessing state properties using dynamic keys, ensuring that TypeScript correctly identifies and enforces the types of state properties.
Changes Implemented:
Type-Safe Iteration:
Replaced the direct for...in loop with a more type-safe iteration using Object.prototype.hasOwnProperty.call() to avoid errors related to iterating over unexpected properties.
Non-null Assertion:
Added a non-null assertion (payload[k]!) to ensure that payload[k] is not undefined when updating the state, preventing runtime issues.
Improved Type-Safe Access:
Used const k = key as keyof S to inform TypeScript that key is a valid key of S, improving the type inference and ensuring that currentState[k] and payload[k] are correctly typed.
Functional Consistency:
The changes maintain the core functionality of the hook while improving its safety and performance, ensuring that it tracks dependencies and triggers re-renders as intended.
This PR does not alter the primary behavior of the hook but makes the code more robust and type-safe, improving maintainability.