typescript - 在 TypeScript 中为 Animated.View 使用 createRef 时如何创建类型
问题描述
在 React Native 组件中创建 ref 时,如果您没有提供正确的类型,Typescript 会抱怨它。
image = React.createRef();
state = {
x: new Animated.Value(0),
y: new Animated.Value(0),
};
render() {
const {x, y} = this.state;
const imageStyle = {left: x, top: y};
return (
<Animated.View
ref={this.image} // I got warning message here
{...this.responder}
style={[styles.draggable, imageStyle]}>
{this.props.children}
</Animated.View>
)
}
const styles = StyleSheet.create({
draggable: {
position: 'absolute',
height: itemWidth,
width: itemWidth,
},
});
我收到了这个警告
输入'{孩子:ReactNode; 样式:({左:值;顶部:值;} | {位置:“绝对”;高度:数字;宽度:数字;})[];当前:未知;参考:参考对象;}' 不可分配给类型 'IntrinsicAttributes & AnimatedProps & { children?: ReactNode; }'。类型'IntrinsicAttributes & AnimatedProps & { children?: ReactNode; 上不存在属性'ref' }'
createRef
线需要像
image = React.createRef<some_type>();
但我不知道如何创建这个some_type
解决方案
更新@types/react-native
版本解决了这个问题。
"@types/react-native": "^0.61.15", -> "@types/react-native": "^0.63.37",
推荐阅读
- java - Android 搜索小部件在单击后退按钮时失去焦点
- docker - 如何在 Traefik 代理后面安装 Harbor
- google-cloud-platform - gsutil ls 命令给出“OSError:权限被拒绝”
- asp.net-core-mvc - ASP.NET Core v1.1 中的 IdentityServer 和 Simple Injector 集成
- javascript - 如何通过反应上下文api传递多个状态
- javascript - 数据更新时Vue Chart JS重新渲染图表
- django - 如何从模型函数中获取数据以显示在 Django Admin 中?
- java - 是否可以使用 Log4J 之类的参数编译 Java 字符串?
- vue.js - Nuxt 当 asyncData 调用不返回数据时,立即显示错误
- c++11 - 为什么行为会随着 emplace_back / push_back 顺序的变化而变化?为什么构造函数的数量增加了?