首页 > 解决方案 > 什么首先触发了本机反应?

问题描述

我有一个反应原生组件,其中包含WillFocuscomponentDidMount函数。我的问题是,如果我导航到该组件,首先触发哪个功能?'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')
    }
}

标签: javascriptreact-nativecomponentsjsx

解决方案


React Navigation 向订阅它们的屏幕组件发出事件。

componentDidMount: 一旦我们的所有子元素和我们的组件实例都安装到本机 UI 上,就会调用此方法。当这个方法被调用时,我们现在可以访问 Native UI(DOM、UIView 等)、访问我们的子 refs 以及潜在地触发新的渲染通道的能力。

willFocus: 屏幕将聚焦。

根据定义,willFocus 将在 ComponentDidMount 之后调用,因为它挂载了所有 UI 组件。


推荐阅读