Muh*_*mad 7 react-native react-hooks
我已将我class component的functional 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)
你不见了.current。
参见工作示例:https : //snack.expo.io/@zvona/lottie-and-useref-example
const onPress = () => {
animation.current.play();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2445 次 |
| 最近记录: |