Navigation addListener在await action之后不执行



我正在听React native中关于导航的焦点事件&使用@react-navigation^5.x.xcomponentDidMount中。如果只写了这个动作,那么它就能很好地工作,并执行其中的代码。

但在我的情况下,我正在从API(也通过mock await尝试)获取数据,并在添加焦点侦听器之前使用await。这是第一次没人听。但是当我转到另一个页面并返回时,它开始工作/执行。

这是我的代码片段。

async componentDidMount() {

await new Promise(resolve => setTimeout(resolve, 3000)); // 3 sec
navigation.addListener('focus', async (data) => {
console.log('This block not works for first time when I come on page.')
})
}

编辑-即使我删除await并将整个块置于超时状态,那么它也会停止执行焦点回调。

我不认为你可以使用async的生命周期试试这个代替:

componentDidMount() {
const getData = async (params) => {
await new Promise(resolve => setTimeout(resolve, 3000)); // 3 sec
navigation.addListener('focus', async (data) => {
console.log('This block not works for first time when I come on page.')
})
}
getData()
}

最新更新