useRef 钩子不适用于 lottie-react-native

Muh*_*mad 7 react-native react-hooks

我已将我class componentfunctional component钩子升级到钩子,但现在 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>
     );
   }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码,它是类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>
   );
}
Run Code Online (Sandbox Code Playgroud)