【发布时间】:2020-05-02 22:01:43
【问题描述】:
我有以下自定义钩子
function useConstant(fn) {
const ref = React.useRef()
if (!ref.current) {
ref.current = fn()
}
return ref.current
}
将其移植到reasonml似乎很难,我必须使用两次类型转换,理想的方式是什么?
external toAny: 'a => 'b = "%identity";
external toBool: 'a => bool = "%identity";
let useConstant = (fn: unit => 'a) => {
let ref: React.Ref.t('a) = toAny(React.useRef());
if (!toBool(React.Ref.current(ref))) {
React.Ref.setCurrent(ref, fn());
};
React.Ref.current(ref);
};
【问题讨论】:
标签: reason reason-react