react-native - useRef 钩子(Hook)不适用于 lottie-react-native

标签 react-native react-hooks

我已经升级了我的class componentfunctional component钩子(Hook),但现在 ref 不起作用。

之前:

class A extends Component{

   animation = null;

   onPress = ()=> {
      this.animation.play();
   }

   render(){
     return (
        <View>
          <LottieView
            source={require('./file.json')}
            ref={animation => {this.animation = animation}}
          />
          <Button onPress={this.onPress} title='click me' />
        </View>
     );
   }
}

以上代码它是类Component效果很好。

但是 以下代码升级 不起作用。

更新代码:
const A = props => {

   const animation = useRef(null);

   const onPress = ()=> {
      animation.play();
   }

   return (
        <View>
          <LottieView
            source={require('./file.json')}
            ref={animation}
          />
          <Button onPress={onPress} title='click me' />
        </View>
   );
}

最佳答案

你错过了.current .

参见工作示例:https://snack.expo.io/@zvona/lottie-and-useref-example

const onPress = () => {
  animation.current.play();
}

关于react-native - useRef 钩子(Hook)不适用于 lottie-react-native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58283848/

相关文章:

javascript - eslint 没有根据 .eslintrc 文件中的规则在 VSCode 中给出错误

android - React native 和 Android 7 支持与 NDK 限制

react-native - 您可以将具有 native 依赖项的库添加到 Expo react native 项目吗?

react-native - 世博会构建 :ios fails with "Error while gathering & validating credentials"

reactjs - 如何在 withFormik() 形式中使用 useMutation 钩子(Hook)

javascript - 如何使用上下文提供程序包装 React-router

reactjs - 你如何使用 react-apollo-hook 中的 useMutation 来执行删除突变?

javascript - 每次输入时的输入字段都失去焦点

reactjs - 如何在 onChange 之后清除 Material-ui 中自动完成的输入?

android - 如何在 WebStorm 中运行 Android 虚拟设备