React-native-tab-view: Update component when changing tab.

Created on 22 May 2017  路  7Comments  路  Source: satya164/react-native-tab-view

Hello,

In my use case I have a tab for settings and a tab for a recording button. In the settings tab i will update a language text input which will export some variable saying what the language has been changed to and then when i navigate back to the recorder view i will need to re initialise a websocket connection for the recorder with this edited language variable.

The problem for me is that, when I go back to the recorder view, it seems that it only re-renders and doesn't re update the whole component. Am I missing something in order to make this happen? any help would be appreciated.

Thanks.

Most helpful comment

If you want to run something when the tab is in focus, pass focused as a prop:

renderScene = ({ route, focused }) => {
  ...
  return <MyScene focused={focused} />
  ...
};

Then in MyScene, check for the prop and do what you need

componentDidMount() {
  if (this.props.focused) {
    // do something
  }
}

componentDidUpdate(prevProps) {
  if (prevProps.focused !== this.props.focused && this.props.focused) {
    // do something
  }
}

All 7 comments

Have read the link but I am still confused as to how to do this. Could you give me any further advice please.

I'm not sure what's the problem you are facing. Can you put a demo on https://snack.expo.io/ ?

I have code in componentWillMount() (tried componentDidMount() as well) that sets up a recorder (runs objective c code). When I make a change in the settings tab, and then come back to the recorder tab, I want it to run this set up code before rendering again (like it would if i refreshed on that tab).

https://snack.expo.io/H1rP361WZ So simply I would like that initialising code that is ran in setup to run again once that tab has been chosen (Recorder tab).

If you want to run something when the tab is in focus, pass focused as a prop:

renderScene = ({ route, focused }) => {
  ...
  return <MyScene focused={focused} />
  ...
};

Then in MyScene, check for the prop and do what you need

componentDidMount() {
  if (this.props.focused) {
    // do something
  }
}

componentDidUpdate(prevProps) {
  if (prevProps.focused !== this.props.focused && this.props.focused) {
    // do something
  }
}

Not too sure how it works but it does. Thanks :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

satya164 picture satya164  路  3Comments

hyochan35 picture hyochan35  路  3Comments

compojoom picture compojoom  路  4Comments

f6m6 picture f6m6  路  3Comments

karthikeyansundaram2 picture karthikeyansundaram2  路  3Comments