javascript - Untyped function calls may not accept type arguments error on generic custom hook
问题描述
I am using this generic hook to return previous props, it was working fine but all of a sudden it is giving "Untyped function calls may not accept type arguments." error.
function usePrevious<T>(value: T): T | undefined {
const ref = useRef<T>();
useEffect(() => {
ref.current = value;
});
return ref.current;
}
const TestingContainer: React.FC<Props> = (props) => {
...
usePrevious<Props>(props)
...
}
解决方案
你必须给props
参数Props
类型
请参阅下面的 LIVE 示例或代码
/**
* stubs - start
*/
interface Props {
someProp: string;
}
interface MutableRefObject<T> {
current: T;
}
function useRef<T>(initialValue: T): MutableRefObject<T>;
function useRef<Props>() {
return mutObj;
}
const mutObj: MutableRefObject<Props> = {
current: {
someProp: '123',
},
}
/**
* stubs - end
*/
function usePrevious<T>(value: T): T | undefined {
// in order to pass validations in this playground
const ref = useRef<T>(value);
// in order to pass validations in this playground
/* useEffect(() => {
ref.current = value;
}); */
return ref.current;
}
const TestingContainer = (props: Props) => {
usePrevious<Props>(props)
}
请让我知道它是否适合您)
推荐阅读
- java - 如何在世界坐标空间中绘制文本?
- python - 如何随机获取最新浏览器版本的用户代理?
- javascript - 我设置边距的功能无法始终如一地工作
- python - 立即处理异步响应
- python - 将 Flask 与 SQLAlchemy 和 Dash 一起使用
- maven - 在远程服务器上部署之前如何更改一些 java 变量?
- java - 如何在其中一个模块中构建没有主类的多模块 Maven 项目
- javascript - TypeScript 有 NaN 类型吗?
- android - MVVM RXAndroid 无法在后台线程上调用 setValue
- mysql - 在 Sequelize 中返回具有更新关联的对象