React Native 组件中的 onEnter/onExit 方法 (react-native-router-flux) [英] onEnter/onExit method in React Native Component (react-native-router-flux)

查看:70
本文介绍了React Native 组件中的 onEnter/onExit 方法 (react-native-router-flux)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我可以在路由器定义中我的应用程序的根目录中使用 onEnter/onExit 方法,它工作得非常好:

So I can use the onEnter/onExit method at the root of my app in the router definitions and it works perfectly fine:

<Scene key="arena" hideNavBar={true}  onEnter={() => console.log("Entered")} component={ArenaPage} />

有什么办法可以在组件内部执行此操作,以便我可以更新本地状态??

Is there any way I can do this inside the component itself so that I can update the local state??

export default class ArenaPage extends Component {
    onEnter () {
        this.setState(...)
    }
    // Render blah blah blah...
}

如果不可能,是否有在离开场景导航时触发 componentWillUnmount (Actions.[key])

If not possible, Is there anyway to trigger componentWillUnmount when navigating away from Scene (Actions.[key])

推荐答案

您可以使用 onEnteronExit 方法来获得您想要的结果.反过来可以像这样访问this.

You can use onEnter and onExit methods to get the result you want. And in turn can access this like so.

import { Actions } from 'react-native-router-flux'

class Home extends Component {
  static onEnter() {
     // homeSceneKey needs to be the same key that you use to configure the Scene
     Actions.refs.homeSceneKey.getWrappedInstance().myFunction()
  };

  myFunction = () => {
     // have access to this (props and state) here
     this.blah = "what ever you want to do"
  }
}

希望能帮到你

这篇关于React Native 组件中的 onEnter/onExit 方法 (react-native-router-flux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆