You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Spring中useSpring返回值的Props类型定义问题

React Spring中useSpring返回值的正确类型定义

你可以直接使用react-spring提供的SpringValue类型来定义这个props,步骤如下:

  1. 先从react-spring导入该类型:
import { SpringValue } from 'react-spring';
  1. 在子组件的Props接口里这样定义:
interface ChildComponentProps {
  dashArrayAnimation: SpringValue<{ strokeDasharray: number }>;
}

另一种推导方式(可选)

如果你不想手动写属性类型,也可以用ReturnType来自动推导useSpring的返回类型:
在父组件中定义类型:

type DashArrayAnimationType = ReturnType<typeof useSpring<{ strokeDasharray: number }>>;

然后子组件Props直接使用这个类型:

interface ChildComponentProps {
  dashArrayAnimation: DashArrayAnimationType;
}

这里的核心是,SpringValue的泛型参数要和你useSpring里定义的动画属性结构对应——你的动画只用到了strokeDasharray这个数值类型的属性,所以泛型填{ strokeDasharray: number }就完全匹配了。

内容的提问来源于stack exchange,提问作者Talha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 10:01:18