DEV Community

Cover image for Fixing `Deprecated: React.Ref.setCurrent`
Alain
Alain

Posted on

2 1

Fixing `Deprecated: React.Ref.setCurrent`

I keep finding myself having looking this up so I'm writing this note to myself.

Bad Code

This code throws a warning:

let useIsMounted = () => {
  let ref = React.useRef(false);

  React.useEffect0(() => {
    ref->React.Ref.setCurrent(true);
    Some(() => ref->React.Ref.setCurrent(false));
  });
  ref;
};
Enter fullscreen mode Exit fullscreen mode

Warning

Warning 3: deprecated: React.Ref.setCurrent
Please directly assign to ref.current insteadocamllsp
Enter fullscreen mode Exit fullscreen mode

The Fix

In ReasonML/Reason-React

let useIsMounted = () => {
  let ref = React.useRef(false);

  React.useEffect0(() => {
    ref.current = true;
    Some(() => ref.current = false);
  });
  ref;
};
Enter fullscreen mode Exit fullscreen mode

In Rescript

let useIsMounted = () => {
  let ref = React.useRef(false)

  React.useEffect0(() => {
    ref.current = true
    Some(() => ref.current = false)
  })
  ref
}

Enter fullscreen mode Exit fullscreen mode

Yes, they are the same except for the commas which the compiler will remove for you anyway.

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More