While working on a particular task I came up with an idea to create my own usePreviousValue hook, just for fun. I know there are already a bunch of pre-existing solutions like react rooks etc.
Initially I only worked with primitive values and hook behaved properly. However when I introduced reference values suddenly it started to behave differently.
Logic for the hook is very simple, store two refs and try to keep previous to be always one step behind the current value.
Minimal example can be found on this link https://codesandbox.io/p/sandbox/use-previous-value-sandbox-m6d56m
Upon clicking on the button to increase the value second time, previous and current refs get the same value. I am very curious to know how react is handling Ref(a) access inside a render call since I am not using Effects in anyway.
Any insights on this matter will be highly appreciated. Maybe is just an error from my side, even though I have spent quite some time to try to fully understand why its breaking.