【问题标题】:React Native - How can I use ref after deprecation?React Native - 弃用后如何使用 ref?
【发布时间】:2021-01-03 05:02:30
【问题描述】:

我正在使用 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

【问题讨论】:

标签: android reactjs react-native ref


【解决方案1】:

对于功能组件

在反应中使用 ref 钩子

const alertRef = useRef(null);

然后像这样使用它

<MyAlert ref={alertRef />

对于类组件

在构造函数中创建引用

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

然后像这样使用它

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

【讨论】:

    猜你喜欢
    • 2020-08-25
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    相关资源
    最近更新 更多