4 ms·
Symbols don't solve this without extra closure wrapper and Hook "instantiation" (described in flaw #5). Passing a Symbol to custom Hook from outside also doesn
by danabramov 8y ago
Symbols don't solve this without extra closure wrapper and Hook "instantiation" (described in flaw #5).
Passing a Symbol to custom Hook from outside also doesn't work because a custom Hook may have more than one state.
Try to convert the `useSubscription` example to your proposed API (and don't forget effects would also need "IDs") and you'll see what I mean.
- k__ 8y agoCouldn't a custom hook have a map of maps that keep track of this? The first map gets the symbol that was passed to the custom hook as keys and the maps inside that map would use symbols only the custom hook knows about. I think your solution is superior in that is more concise and I at least think I understand why you went that way. I'm just trying to understand why the symbol approach wouldn't work.
- danabramov 8y agoYeah you could do this but this “breaks copy paste” (one of the flaws). We want creating a custom Hook to feel exactly like extracting a function. It should feel easy and you shouldn’t need to mess with maps and symbols.
- k__ 8y agoOkay, DX is an important selling factor, so your approach seems reasonable to me.
- danabramov 8y agoRuntime performance is another one. Map lookups aren’t free, especially if there’s a whole bunch of them happening in every component on every render.
- k__ 8y agoYes, the order restriction buys a lot of things rather cheaply. And while I trust the React teams judgement, I teach people React and they often question the "why".