Search code examples
listviewreact-nativecomponentsdelayrerender

Function called after render react native


i have a load function which loads my data to my ListView datasource and as you might know ListView wont always rerender with changing state and i need to forceupdate it this load function works really well being called by a button but i want it to auto load but calling forceupdate is not available in componentwill/did mount functions what can i do ? i just want my function to be called with a little delay after render. the problem is this.props.navigation is not an object before render and cant be used in componentwill/did mount.

 //   UNSAFE_componentWillMount() {
//   this.makeScene();
// }

makeScene(){
var count = Object.keys(window.contents[this.props.navigation.getParam('name')]).length;
for(let i = 0 ; i < count ; i ++){
  global.subnum[i] = i;
  global.lowersubjects[i] = window.contents[this.props.navigation.getParam('name')][i].subject;
  global.lowerdescriptions[i] = window.contents[this.props.navigation.getParam('name')][i].description;
  global.lowerimageurl[i] = window.contents[this.props.navigation.getParam('name')][i].imageurl;
  global.author[i] = window.contents[this.props.navigation.getParam('name')][i].author;
  global.eventdate[i] = window.contents[this.props.navigation.getParam('name')][i].eventdate;
}
this.setState({
  subnum : global.subnum
});
this.forceUpdate();
}    

uncommenting will cause my app buttons and render stop working.


Solution

  • 3 things:

    Firstable, getParam is missing an argument that is a default value if the specified field is empty or do not exists :

    var name = this.props.navigation.getParam('name', 'defaultName')

    With name you won't have to call it all time to access your array cell.

    Second, What is you window.contents ?

    Finaly, every this.setState() call will cause the view to render IF the state or props of your component has changed. You should call your procedure in componentDidMount() and yes, this.props.navigation should be accessible into componentDidMount() since your component is in a navigator I suppose...