【问题标题】:Animated: `useNativeDriver` was not specified issue of ReactNativeBase Input动画:`useNativeDriver` 未指定 ReactNativeBase 输入问题
【发布时间】:2020-07-15 18:52:59
【问题描述】:

我今天(2020 年 4 月 3 日)创建了一个新的 react-native 项目并添加了一个原生基础。然后我尝试使用浮动标签添加输入。它给出了一条警告消息:动画:useNativeDriver 未指定。这是一个必需选项,必须明确设置为truefalse。如果我删除了浮动标签属性或将其更改为stackedLabel,则警告消失了。在出现此警告时,onChangeText 并未被调用。

组件文件

import React from 'react';
import {
  SafeAreaView,
  ScrollView,
  StatusBar,
  StyleSheet,
  View,
} from 'react-native';

import {Input, Item, Label} from 'native-base';

import {Colors} from 'react-native/Libraries/NewAppScreen';

declare var global: {HermesInternal: null | {}};

const App = () => {
  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
        <ScrollView
          contentInsetAdjustmentBehavior="automatic"
          style={styles.scrollView}>
          <View style={styles.body}>
            <Item floatingLabel>
              <Label>Test</Label>
              <Input onChangeText={text => console.log(text)} />
            </Item>
          </View>
        </ScrollView>
      </SafeAreaView>
    </>
  );
};

package.json

{
  "name": "formtest",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint . --ext .js,.jsx,.ts,.tsx"
  },
  "dependencies": {
    "native-base": "^2.13.12",
    "react": "16.11.0",
    "react-native": "0.62.0"
  },
  "devDependencies": {
    "@babel/core": "^7.6.2",
    "@babel/runtime": "^7.6.2",
    "@react-native-community/eslint-config": "^1.0.0",
    "@types/jest": "^24.0.24",
    "@types/react-native": "^0.62.0",
    "@types/react-test-renderer": "16.9.2",
    "@typescript-eslint/eslint-plugin": "^2.25.0",
    "@typescript-eslint/parser": "^2.25.0",
    "babel-jest": "^24.9.0",
    "eslint": "^6.5.1",
    "jest": "^24.9.0",
    "metro-react-native-babel-preset": "^0.58.0",
    "react-test-renderer": "16.11.0",
    "prettier": "^2.0.2",
    "typescript": "^3.8.3"
  },
  "jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  }
}   

【问题讨论】:

    标签: javascript reactjs react-native native-base


    【解决方案1】:

    似乎是当前 nativebase.io 版本的一个已知错误:https://github.com/GeekyAnts/NativeBase/issues/3109

    附加信息,问题到底是什么:https://reactnative.dev/blog/2017/02/14/using-native-driver-for-animated#how-do-i-use-this-in-my-app

    【讨论】:

      【解决方案2】:

      只需在文件夹 ~\node_modules\native-base\dist\src\basic\ 上搜索 animated.timing 并手动添加 useNativeDriver: true or false

      【讨论】:

      • 如果您进行这样的更改,您很可能希望使用“补丁包”模块来生成您所做更改的补丁,并在重新安装 node_modules 时自动应用它们
      【解决方案3】:

      只需将useNativeDriver: true 添加到动画配置中即可。

      const [animatePress, setAnimatePress] = useState(new Animated.Value(1))
      
      const animateIn = () => {
        Animated.timing(animatePress, {
          toValue: 0.5,
          duration: 500,
          useNativeDriver: true // Add This line
        }).start();
      }
      

      更新

      解决方案:

      正如警告所说,我们需要明确指定useNativeDriver 选项并将其设置为truefalse

      1-动画方法

      参考Animated doc,有动画类型或合成功能,例如Animated.decay()Animated.timing()Animated.spring()Animated.parallel()Animated.sequence(),指定useNativeDriver

      Animated.timing(this.state.animatedValue, {
        toValue: 1,
        duration: 500,
        useNativeDriver: true, // Add this line
      }).start();
      

      2- 动画组件

      Animated 使用上述包装器导出以下动画组件:

      • Animated.Image
      • Animated.ScrollView
      • Animated.Text
      • Animated.View
      • Animated.FlatList
      • Animated.SectionList

      使用 Animated.event() 时,将 useNativeDriver: false/true 添加到动画配置中。

      <Animated.ScrollView
        scrollEventThrottle={1}
        onScroll={Animated.event(
          [{ nativeEvent: { contentOffset: { y: this.state.animatedValue } } }],
          { useNativeDriver: true } // Add this line
        )}
      >
        {content}
      </Animated.ScrollView>
      

      【讨论】:

      • 嗨,我可以知道在哪里可以找到和编辑 Animated.timing 吗?
      • 你不能简单地在你的答案中添加另一个答案而不指定他/她的名字
      • @Nisharg Shah - 这是你的权利,但我在 github.com 中读到:github.com/GeekyAnts/NativeBase/issues/… 我从我的答案中删除了它,正如我告诉我在 Github 中读到的那样
      • 没有问题,但是如果您在有人在您的帖子中添加相同答案后更新您的答案,您必须指定名称,这就是为什么我告诉您以及这些 SO 的常规行为
      【解决方案4】:

      这主要是找到 Animated.timing 或 Animated.spring 的所有实例并添加 useNativeDriver:对配置正确。

      【讨论】:

        【解决方案5】:

        使用类 Component 只需在 componentDidMount() 中添加 Animated.timing 并将 useNativeDriver 定义为 true 或 false

        class App extends Component {
        
            animatedValue = new Animated.Value(0);
        
            componentDidMount() {
                Animated.timing(this.animatedValue,
                    {
                        toValue: 1,
                        duration: 1000,
                        useNativeDriver: true
                    }).start();
            }
        
            render() {
                return (
                    <View style={styles.container}>
                        <View style={styles.squareBG}/>
                        <Animated.View style={[styles.square, {opacity: this.animatedValue}]}/>
                    </View>
                );
            }
        }
        

        【讨论】:

          【解决方案6】:

          我刚刚在我的App.js 中添加了这个并为我工作:)

          import { YellowBox } from 'react-native';
          
          YellowBox.ignoreWarnings([
            'Animated: `useNativeDriver` was not specified.',
          ]);
          

          【讨论】:

            【解决方案7】:

            长期以来一直面临同样的问题,到 2021 年仍然没有来自原生开发者的更新。

            同时使用解决方法来避免useNativeDriver 的黄色警告。

            更新 RN V0.63 以上

            YellowBox 现在已更改并替换为 LogBox

            功能性

            import React, { useEffect } from 'react';
            import { LogBox } from 'react-native';
            
            useEffect(() => {
                LogBox.ignoreLogs(['Animated: `useNativeDriver`']);
            }, [])
            

            基于类

            import React from 'react';
            import { LogBox } from 'react-native';
            
            componentDidMount() {
                LogBox.ignoreLogs(['Animated: `useNativeDriver`']);
            }
            

            在下面更新 RN V0.63

            功能性

            import React, { useEffect } from 'react';
            import { YellowBox } from 'react-native';
            
            useEffect(() => {
                YellowBox.ignoreWarnings(['Animated: `useNativeDriver`']);
            }, [])
            

            基于类

            import React from 'react';
            import { YellowBox } from 'react-native';
            
            componentDidMount() {
                YellowBox.ignoreWarnings(['Animated: `useNativeDriver`']);
            }
            

            【讨论】:

            • 这帮助了我
            • 功能性是什么意思?我应该在哪里添加这个?这对消息的所有外观都有效吗?
            • React 是用两种风格编写的,基于函数的和基于类的,你必须在那些你想忽略那些警告的地方添加代码
            • Idk 我只是说这是一个警告,隐藏警告与修复警告不同,是吗?但我也将暂时隐藏警告,因为它还没有修复。
            【解决方案8】:

            当使用Animated.spring 或任何其他动画时,指定useNativeDriver: true of useNativeDriver: false

            例子:

            Animated.spring(this.position, {
                toValue: { x: 0, y: 0 },
                useNativeDriver: true,
            }).start();
            

            Animated.springonPanResponderRelease 函数中被调用。

            【讨论】:

              【解决方案9】:

              在 react native SDK 39 中,您必须编写以下代码:

              LogBox.ignoreLogs(['Animated: `useNativeDriver` was not specified.']);
              

              【讨论】:

                【解决方案10】:

                使用以下代码避免usenativedriver的警告信息

                  Animated.timing(new Animated.Value(0), 
                        {
                          toValue: 1,
                          duration: 500,
                          easing: Easing.linear,
                          useNativeDriver: false //make it as false
                        }).start();
                

                【讨论】:

                  【解决方案11】:

                  native-base 在 1 月份修复了这个问题。至少获得 v2.15.2

                  yarn add native-base@^2.15.2

                  发行说明:https://github.com/GeekyAnts/NativeBase/releases/tag/v2.15.2

                  【讨论】:

                    猜你喜欢
                    • 2010-12-06
                    • 1970-01-01
                    • 1970-01-01
                    • 2011-10-03
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-05-26
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多