1

我正在尝试创建一个简单的 React Native 动画:

const flexValueForSelected = (selected:boolean) => selected ? 2 : 1;
const [flexValue] = useState(new Animated.Value(flexValueForSelected(selected)));
useEffect(() => {
    console.log('changing ' + route.routeName + ' to ', selected)
    Animated.spring(flexValue, { toValue: flexValueForSelected(selected)}).start();
    // Animated.timing(flexValue, {duration:300, easing: Easing.linear, toValue: flexValueForSelected(selected)}).start();
}, [selected])
return <Animated.View key={route.routeName} style={{flexGrow: flexValue, ...styles.touchable}}>
            ...
       </Animated.View> 

selected它位于功能组件内部,并且在属性更改时触发。该函数被正确调用(在selected更改时触发,不会多次触发或值不更改等),但我收到以下错误:

RCTJSONStringify() encountered the following error: Invalid number value (NaN) in JSON write

我还提供了各种选项,例如文档中记录摩擦/张力,但我仍然遇到相同的错误,并且无论我尝试哪种组合,我都会遇到 TypeScript linter 错误:

Argument of type '{ friction: number; tension: number; toValue: number; }' is not assignable to parameter of type 'SpringConfig'.
  Object literal may only specify known properties, and 'friction' does not exist in type 'SpringConfig'.ts(2345)

当我切换到线性动画时,例如Animated.timing(flexValue, {duration:300, easing: Easing.linear, toValue: flexValueForSelected(selected)}).start();它工作得很好,所以问题是弹簧动画特有的(这是我在视觉上想要实现的)。

我究竟做错了什么?(我在 React Native 0.61.2 上)

4

1 回答 1

1

确保您是从而不是Animated从.react-nativereact-native-reanimated

于 2020-07-17T22:07:35.053 回答