onEnter/onExit method in React Native Component (react-native-router-flux)

╄→尐↘猪︶ㄣ 提交于 2019-12-22 10:40:31

问题


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...
}

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


回答1:


Please check latest react-native-router-flux:beta.27, now you can define onEnter, onExit methods as your react component methods.

class Home extends Component {
  static onEnter() {
    console.log('On Focus Enter')
  };
  static onExit() {
    console.log('On Focus Exit')
  }
}



回答2:


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"
  }
}

Hope this helps




回答3:


Simple use componentWillUnmount, in the arenaPage component.

componentWillUnmount() {
    // add your code here.
}

Because that will run when navigation away from the component. I am using it to remove error messages. And that works.



来源:https://stackoverflow.com/questions/47573777/onenter-onexit-method-in-react-native-component-react-native-router-flux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!