Brien Crean
Brien Crean

Reputation: 2648

react-native-router-flux - trigger function on specific route load

I am trying to trigger a function whenever a specific route within a router loads. I am using react-native-router-flux 2.x and ReactNative 0.21 (Can't update to 3.x right now).

For example below I just want to trigger a function whenever screen2 is loaded. Is this possible? I tried using onPush but it didn't seem to work.

The reason I need this is because I cannot use componentDidMount, componentWillMount for the screen2 component as it will only load the first time the component is called. I need a function to trigger every time the route is selected. Thanks!

  <Route name="screen1" initial={!needSignIn}>
    <Router footer={TabBar} hideNavBar={true} >
      <Route name="screen1" hideNavBar={true} title="Screen1" schema="tab" component={Screen1} />
      <Route onPush={()=>{console.log("onPush is called!");}} name="screen2" hideNavBar={true} title="Screen2" schema="tab" component={Screen2} />
      <Route name="screen3" hideNavBar={true} title="Screen2" schema="tab" component={Screen3} />
    </Router>
  </Route>

Upvotes: 4

Views: 2805

Answers (1)

Wesley Gormley
Wesley Gormley

Reputation: 134

The only events that can fire events with react-native-router-flux are onLeft and onRight see documentation. The only to fire off functions are lifecycle functions. You said that component(Did/Will)Mount wont work, what about the others, like componentWillReceiveProps? Use componentWillMount for the first render, and componentWillReceiveProps for every other render.

Upvotes: 2

Related Questions