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;
};
Warning
Warning 3: deprecated: React.Ref.setCurrent
Please directly assign to ref.current insteadocamllsp
The Fix
let useIsMounted = () => {
let ref = React.useRef(false);
React.useEffect0(() => {
ref.current = true;
Some(() => ref.current = false);
});
ref;
};
In Rescript
let useIsMounted = () => {
let ref = React.useRef(false)
React.useEffect0(() => {
ref.current = true
Some(() => ref.current = false)
})
ref
}
Yes, they are the same except for the commas which the compiler will remove for you anyway.
Top comments (0)