React Native - 弃用后如何使用 ref?

React Native - How can I use ref after deprecation?

我正在使用 ref 来显示自定义警报,但我收到了关于弃用的警告,那么我该如何使用 ref 或任何替代品?

<MyAlert ref="alert" />

this.refs.alert.open(
        errorBody,
        [
            {
                text: pbtn, onPress: () => {
                    this._home_call()
                }
            },
            { text: nbtn, onPress: () => { } }
        ],
        {
            type: 'alert',
            cancelable: false,
        },
    );

反应版本:16.13.1

react-native 版本:0.63.2

对于功能组件

在 React 中使用 ref hook

const alertRef = useRef(null);

然后像这样使用它

<MyAlert ref={alertRef />

对于 class 个组件

在构造函数中创建 ref

constructor() {
  super();
  this.alertRef= React.createRef();
 
}

然后像这样使用它

<MyAlert ref={(ref) => this.alertRef = ref} />