我将 react-spring 与 Typescript 一起使用。当我将本机渲染与 react-spring 一起使用时,我收到 interpolate 函数的错误消息。
“类型‘数字’上不存在属性‘插值’”
我尝试在Spring组件内部props中引入一个接口,但是我无法摆脱各种错误消息。
import * as React from 'react';
import { FC, useState } from 'react';
import { Spring, animated as a } from 'react-spring/renderprops';
interface Props {
onClick: Function;
}
/*interface SpringProps {
scale: number | Scale;
}
interface Scale {
interpolate: Function;
}*/
const SpringButton: FC<Props> = ({ onClick }) => {
const [pressed, setPressed] = useState(false);
return (
<Spring native from={{ scale: 1 }} to={{ scale: pressed ? 0.8 : 1 }}>
{(props /*: SpringProps*/) => (
<a.button
style={{
height: '100px',
width: '100px',
transform: props.scale.interpolate(scale => `scale(${scale})`)
}}
onMouseDown={() => setPressed(true)}
onClick={e => {
setPressed(false);
onClick(e);
}}
onMouseLeave={() => setPressed(false)}
>
Click me
</a.button>
)}
</Spring>
);
};
export default SpringButton;