如何修复错误: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(),请参见上面的链接以获取详细信息)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...