перехватчики useRef не работают с lottie-реагировать-нативно - PullRequest
1 голос
/ 08 октября 2019

Я обновил свои class component до functional component крючки, но сейчас ссылка не работает.

До:

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>
     );
   }
}

The код выше , который является классом 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>
   );
}

1 Ответ

2 голосов
/ 08 октября 2019

Вам не хватает .current.

См. Рабочий пример по адресу: https://snack.expo.io/@zvona/lottie-and-useref-example

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