javascript - 什么首先触发了本机反应?
问题描述
我有一个反应原生组件,其中包含WillFocus和componentDidMount函数。我的问题是,如果我导航到该组件,首先触发哪个功能?'WillFocus' 或 'componentDidMount' 示例代码如下所示
class Notifications extends Component {
static navigationOptions = {
header: null
}
constructor(props) {
super(props);
const{navigation}=this.props
this.state = {
highlightHome : true,
highlightNotifications: true,
}
}
willFocus = [this.props.navigation.addListener(
'willFocus',
payload => {
console.log('willFocus')
}
)]
componentDidMount() {
console.log('componentDidMount')
}
}
解决方案
React Navigation 向订阅它们的屏幕组件发出事件。
componentDidMount: 一旦我们的所有子元素和我们的组件实例都安装到本机 UI 上,就会调用此方法。当这个方法被调用时,我们现在可以访问 Native UI(DOM、UIView 等)、访问我们的子 refs 以及潜在地触发新的渲染通道的能力。
willFocus: 屏幕将聚焦。
根据定义,willFocus 将在 ComponentDidMount 之后调用,因为它挂载了所有 UI 组件。
推荐阅读
- php - 为什么在 if 语句中使用 PHP 打印变量?
- php - 字符编码在
- c# - 我可以在 wpf 应用程序中使用网络核心库吗?
- java - 使用作为参数给出的比较器对对象列表进行排序的类方法(Java)
- c# - 无法访问客户端应用程序
- sql - 如果 ID 存在于另一个表中,则插入行
- nginx - 如何在 Nginx 上配置具有相同 URL 的多个站点
- reactjs - 将事件处理程序放在 NextJS _document 中——以及 NextJS 页面和 React SPA 之间的关系
- apache-flink - 如何在 Apache Flink 中创建外部目录表
- c++ - CUDA: Fill matrix with results of summation