Feature request: Add a key property to custom hooks to reset state

This issue has been created since 2022-09-11.

We know that we can provide key to React custom components, and when we provide a different key to React custom component on next render, the previous instance associated with that custom component will be gone, all state will be reset, etc. In some situations this is even recommended in react docs.

Would similar feature be useful also for custom hooks? To pass it a key as parameter? And if on next render this key is different, then all state of this custom hook will be destroyed.

Pros:

  • Similar to components in some situations it may be desirable to reset state of custom hook. With this feature you can do it automatically, without manually dealing with such situation.

Possible difficulties

  • After giving it some more thought I think it might be tricky how to pass that key to the custom hook. It must go as a parameter, but then users won't be able to use it inside. Also how should the custom hook guess which parameter is the key, etc.
vkurchatkin wrote this answer on 2022-09-12

"Custom hooks" are not something that React can control or is aware of. It is just a plain function that is called normally.
So, if you want to add some sort of key as an argument of your custom hook, you have to handle it yourself:

function myCustomHook({ key }) {
   const [state, setState] = useState(initialState);

  useEffect(() => {
    setState(initialState);
  }, [key]);
}
gmoniava wrote this answer on 2022-09-12

Yeah also like I mentioned after giving some more thought, although this might be useful (because like components hooks maintain state too and I might want to reset it), not sure how this can be done in practice.

More Details About Repo
Owner Name facebook
Repo Name react
Full Name facebook/react
Language JavaScript
Created Date 2013-05-24
Updated Date 2022-09-26
Star Count 195293
Watcher Count 6655
Fork Count 40426
Issue Count 1113

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Error occured while generating docs 2 2022-01-25 2022-07-09
New Doc Support for PHP : OpenApi / Swagger 0 2022-02-24 2022-09-19
cargo bitbake crashes 4 2021-03-04 2022-05-16
🐛 [firebase_app_check] `.getToken()` is not working for iOS and macOS 3 2022-06-26 2022-08-31
Program fails to create a new rib 3 2020-10-29 2022-09-11
It doesn't launch on River 10 2020-12-27 2022-06-29
Is it possible to integrate local Wake word detection 4 2021-06-01 2022-09-15
Evaluation with identified model 10 2021-10-30 2022-08-05
[FeatureRequest] Non-dynamic parameter estimation 2 2021-11-09 2022-09-26
Cannot work a tile (locked by a puppet city?) 2 2021-12-31 2022-08-02
Add exit node selection 2 2022-04-26 2022-08-21
🐛 BUG: fsonathan... Vite error when using `fs` in external files 9 2022-01-14 2022-04-15
Improve devtools / debugging experience / localhost development for Pi Apps running SDK v2 50 2021-06-29 2022-09-08
Can you do something about this GUI overlap? 1 2022-03-03 2022-09-10
failure to center on 1.7.2 x64 portable 3 2021-10-12 2022-09-05
anchor on hover status - change color 2 2020-04-17 2022-08-13
Fix watch next continous play of tv shows 7 2022-01-03 2022-09-09
php 容器进入后crond服务没有默认开机自启 0 2022-05-16 2022-09-13
Group chat doesn't appear anywhere until dialog_list is called 2 2015-07-20 2022-01-18
Option to view preview definition along with/instead of signature when populating parameters 0 2021-05-24 2022-09-13
Old H logo visible on iMessage unfurl 3 2020-06-03 2022-07-28
"go get -u github.com/nsf/gocode" don't do anything for me. 14 2016-07-01 2022-09-09
running lavalink with multiple ports? 2 2022-07-05 2022-08-19
Does youtubesearch still work normally for lavalink? 8 2022-07-21 2022-08-27
AudioResource memory leak 12 2021-08-07 2022-09-11
OAPIF: api-key not included when requesting `/collections` endpoint 1 2021-12-17 2022-07-30
Escrever a introdução do relatório // Write report's introduction 0 2021-01-21 2022-08-30
test_ext_imgconverter.py fails if ImageMagick is not installed 0 2022-04-24 2022-06-22
[Bug] Beacon shine only at the first window, not others (Emacs 27.1) 0 2021-03-30 2022-09-26
4182 - Fibonacci Sequence 0 2021-12-01 2022-08-07
Selected video duplicates after editing mode 0 2021-09-29 2022-08-29
Tooltip: Hoverable Tooltip correct behaviour 2 2022-07-06 2022-08-22
Uncaught TypeError: Cannot read properties of undefined (reading 'prototype') 5 2021-12-02 2022-09-20
🐛 Bug Report: Last commit and commit loader is hanging although the process finished 1 2022-08-27 2022-08-29
How to use GHC installed by stack? 2 2021-05-30 2022-09-19
Undefined Offset: in Migration/Step/Eav/Data.php 18 2020-05-11 2022-09-16
Backup fails with: rclone","M":"rc: \"operations/cat\": error: object not found","S" backend Minio 8 2022-02-25 2022-09-25
RFE: Support client certificate authentication 0 2022-02-18 2022-09-07
Flaky test teardown: Quota exceeded for quota metric 'Instance delete requests' 2 2021-09-07 2022-09-20
Unable to start Peertube 4.0.0 instance 5 2021-12-12 2022-09-19
检索模型精度 低于 分类模型精度 6 2021-12-13 2022-01-14
Project status 6 2021-06-23 2022-01-15
How to configure primary key? 2 2021-07-06 2022-09-21
Nested filtering is not working 0 2021-08-04 2022-09-24
feat:getSettingByProjectCode改成根据工蜂项目ID查询,主键默认索引,优化慢查询。 0 2022-07-13 2022-08-13
feat: 人工审核插件支持带参数的审批通知 0 2022-07-13 2022-09-01
bug:修复流水线标签重复写入T_PIPELINE_LABEL_PIPELINE后被删除的问题。 0 2022-07-12 2022-08-04
Flaky E2E: Start new post 0 2021-10-21 2022-08-11
SameSite attribute for cookies are not being added 0 2021-05-28 2022-09-23
Homepage as listed on npmjs.com 0 2020-01-06 2022-09-04