Javascript 如何创建ref来调用函数组件的函数?
升级react native 0.60后。 我试着把我所有的类组件都改成函数组件。 对于我的动画,我喜欢使用组件的方法(我知道这并不推荐,但我更喜欢创建状态变量并将其作为道具传递…) 例如,下面是我的课堂作文:Javascript 如何创建ref来调用函数组件的函数?,javascript,reactjs,react-native,methods,ref,Javascript,Reactjs,React Native,Methods,Ref,升级react native 0.60后。 我试着把我所有的类组件都改成函数组件。 对于我的动画,我喜欢使用组件的方法(我知道这并不推荐,但我更喜欢创建状态变量并将其作为道具传递…) 例如,下面是我的课堂作文: class Child extends PureComponent { animateOpen = () => { } render() { return ( <View> {...} </View&g
class Child extends PureComponent {
animateOpen = () => {
}
render() {
return (
<View>
{...}
</View>
);
}
}
类子级扩展PureComponent{
animateOpen=()=>{
}
render(){
返回(
{...}
);
}
}
const PlayButton=({status,onPress})=>{
const childRef=useRef(null);
返回(
);
};
因此,在本例中,如何将子组件编写为功能组件?尝试使用
this.childRef.current.animateOpen
尝试使用this.childRef.current.animateOpen子组件作为功能组件:
const Child = () => {
const animateOpen = () => {}
return (
<View>
{...}
</View>
)
}
const Child=()=>{
常量animateOpen=()=>{}
返回(
{...}
)
}
子组件作为功能组件:
const Child = () => {
const animateOpen = () => {}
return (
<View>
{...}
</View>
)
}
const Child=()=>{
常量animateOpen=()=>{}
返回(
{...}
)
}