【问题标题】:What triggered first in react native?React Native 中首先触发了什么?
【发布时间】:2019-12-16 01:36:57
【问题描述】:

我有一个 react native 组件,它包含 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')
    }
}

【问题讨论】:

    标签: javascript react-native components jsx


    【解决方案1】:

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

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

    willFocus: 屏幕将聚焦。

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

    【讨论】:

      【解决方案2】:

      componentDidMount 先打电话,但在下一次(回来或其他...)只需willFocus 打电话

      【讨论】:

        猜你喜欢
        • 2014-12-19
        • 2015-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-23
        • 2013-02-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多