动画:`useNativeDriver` 不是 ReactNativeBase 输入的指定问题

IT技术 javascript reactjs react-native native-base
2021-05-16 09:22:18

我今天(2020 年 4 月 3 日)创建了一个新的 react-native 项目并添加了一个 native-base。然后我尝试使用浮动标签添加输入。它给出了一条警告消息:动画:useNativeDriver未指定。这是必需选项,必须显式设置为truefalse如果我删除了浮动标签属性或将其更改为 stackLabel 警告消失了。虽然出现此警告,但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>
    </>
  );
};

包.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"
    ]
  }
}   
4个回答

只需添加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明确指定 选项并将其设置为 true or false

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>

长期面临同样的问题,但在 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`']);
}

似乎是当前 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-应用程序

当使用Animated.spring或任何其他动画指定useNativeDriver: trueuseNativeDriver: false

例子:

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

Animated.spring被称为onPanResponderRelease功能。