Animated.Value 的 PropTypes?

Isa*_*aac 3 javascript reactjs react-native react-animated react-proptypes

我在父级定义了动画值如下

const scrollY = new Animated.Value(0);

console.log(scrollY); // 0;
console.log(typeof scrollY); // object;
Run Code Online (Sandbox Code Playgroud)

然后我需要将此值传递给我的组件,如下所示

<ChildComponent animatedValue={scrollY} />
Run Code Online (Sandbox Code Playgroud)

现在我想知道,在ChildComponent,我的道具的正确道具类型应该是什么animatedValue

ChildComponent.propTypes = {
  animatedValue: PropTypes.number.isRequired
};
Run Code Online (Sandbox Code Playgroud)

如果我定义为PropTypes.number我收到警告,这是有道理的,因为 typeofscrollY是对象。

但是由于 eslint 错误我们无法在下面定义,对于对象我们应该使用 PropTypes.shape,但我不知道我应该设置什么形状?

ChildComponent.propTypes = {
  animatedValue: PropTypes.object.isRequired // eslint error
};
Run Code Online (Sandbox Code Playgroud)

Dav*_*haw 5

您可以检查传递的对象是否是类的实例

PropTypes.instanceOf(Animated.Value)
Run Code Online (Sandbox Code Playgroud)