如何修复错误:AnimatedValue:尝试将值设置为undefined-React Native

问题描述

我想在显示和隐藏键盘时调整徽标的大小。 这是imgStyle.js

 import {StyleSheet,Dimensions} from 'react-native';

    const Height = Dimensions.get('screen');
    const height_logo = Height * 0.2;
    const height_logo_small = Height * 0.6;

    const IMAGE_HEIGHT = height_logo;

    const IMAGE_HEIGHT_SMALL = height_logo_small;

这是主要代码..(不是完整代码

     constructor(props) {
            super(props);
         this.imageHeight = new Animated.Value(IMAGE_HEIGHT);
              }
      
    
    componentDidMount() {
        this.keyboardDidShowSub = Keyboard.addListener(
          'keyboardDidShow',this.keyboardDidShow,);
        this.keyboardDidHideSub = Keyboard.addListener(
          'keyboardDidHide',this.keyboardDidHide,);
      }

  

componentwillUnmount() {
        this.keyboardDidShowSub.remove();
        this.keyboardDidHideSub.remove();
      }
      

keyboardDidShow = () => {
        Animated.timing(this.imageHeight,{
          tovalue: IMAGE_HEIGHT_SMALL,duration: 5000,}).start();
      };
  

keyboardDidHide = () => {
    Animated.timing(this.imageHeight,{
      tovalue: IMAGE_HEIGHT,}).start();
  };

这是错误

错误:AnimatedValue:尝试将值设置为未定义

有人有这个错误解决了吗?

解决方法

未从imgStyle.js导出IMAGE_HEIGHT常量

,

尝试将Animated.Value设置为布尔值而不是01时遇到此问题。

需要对此进行更改:

// expanded is a boolean var here
const toggleAnim = useRef(new Animated.Value(!expanded)).current;

对此:

// expanded is a boolean var here
const toggleAnim = useRef(new Animated.Value(expanded ? 0 : 1)).current;

value必须是数字:see documentation

(注意:这同时适用于Animated.ValuesetValue(),请参见上面的链接以获取详细信息)