我已经升级了我的class component
至functional 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/