react.dev icon indicating copy to clipboard operation
react.dev copied to clipboard

usePrevious hook is buggy

Open pgsandstrom opened this issue 7 years ago • 7 comments

https://reactjs.org/docs/hooks-faq.html#how-to-get-the-previous-props-or-state

Using useEffect is dangerous since it is not guaranteed to run. Here is an example where useEffect is not run on each render: https://codesandbox.io/s/react-hooks-useeffect-pt41r?fontsize=14

When useEffect is not triggered, the usePrevious risks causing bugs, like infinite loops.

The hook could instead be written like this, simpler and with no bugs:

export function usePrevious<T>(value: T) {
  const ref = useRef<T>()
  const prevValue = ref.current
  ref.current = value
  return prevValue
}

I wouldn't mind writing a PR myself if someone can confirm that my thinking is right.

pgsandstrom avatar Nov 12 '19 12:11 pgsandstrom

If it helps, I could write up a full example of when the current usePrevious can cause a bug.

pgsandstrom avatar Nov 12 '19 13:11 pgsandstrom

I believe the intention of the hook is that it returns the previous successfully-rendered value. So I think it is correct as written.

Though I'm curious to hear what your use case is that caused you to find a problem with it. Please do share an example.

(I'll also note that your alternative is not necessarily safe for the upcoming concurrent mode, but it depends on the semantics you're expecting.)

sophiebits avatar Jan 01 '20 16:01 sophiebits

(I'll close this as I don't think this is a problem with the docs, but feel free to reply.)

sophiebits avatar Jan 01 '20 16:01 sophiebits

Thank you for the reply! My problem occurred in production code that I cannot share or even access at this moment, but I have reconstructed the code in a contrived example:

https://codesandbox.io/s/react-hooks-useeffect-tm2yh?fontsize=14&hidenavigation=1&theme=dark

Here I have named the hooks usePreviousOldVersion and usePreviousNewVersion. As you can see the old version results in an error, but the new one does not.

Perhaps it is simply the components that was badly designed (our production code was less contrived than my example, but by no means perfect). I can see the value of not saving values from unsuccessful renders. If there is a way to resolve both problems, I unfortunately cannot see it.

pgsandstrom avatar Jan 01 '20 18:01 pgsandstrom

+1.. I have the same issue here, and the usePreviousNewVersion in @pgsandstrom's code sample worked for me. Would be interested if there's a follow-up..

jiayexie avatar Dec 10 '21 23:12 jiayexie

This has tripped us up as well recently.

I believe the intention of the hook is that it returns the previous successfully-rendered value

It would be really helpful to understand a use-case for this. When would someone need to know the value of the previous render? Would this be reliable in concurrent mode?

jjenzz avatar Dec 22 '21 14:12 jjenzz

function usePrevious<T>(value:T, initial:T) {
  const ref = useRef({target: value, previous: initial});

  if (ref.current.target !== value) {
    ref.current.previous = ref.current.target;
    ref.current.target = value;
  }
  return ref.current.previous;
}

I was stuck with one use case, where I need the previous value which does not match with the current value, but not the previous render value, so this code helps me.

MuhammadUsman786786 avatar Jun 02 '23 19:06 MuhammadUsman786786