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.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more