React Meteor How to execute a function after withTracker data arrives?

I am publishing data from server and catching it using withTracker.

export default withTracker(() => {
    let towndatasub = Meteor.subscribe("userTownDataPublisher",Meteor.userId());
  let resourcedatasub = Meteor.subscribe("userResourcePublisher",Meteor.userId());
    
      return{
    townData : Towns.find({"ownerId":Meteor.userId()}).fetch(),
    resourceData : Resources.find({"ownerId":Meteor.userId()}).fetch()
    
      }
    
    })(TownPage);

The problem is i would like to run a function when townData and resourceData arrives.If i call updateResources in componentDidMount i get undefined on this.props.townData and this.props.resourceData

updateResources = () =>{
    
    
          
    Meteor.call("updateUserResources",Meteor.userId(),(err,result)=>{
    if(err){
      console.log(err)
    }else{
      console.log("asdasd");
          
          //console.log(this.props.resourceData); undefined
          // here i will do something with this.props.resourceData
    
    }
    
    })
          }

So where should i call updateResources function to not get undefined ?

Sounds like you can call that function after checking the reactive loading switch. You can get the loading switch like in this snippet:

export default ListPageContainer = withTracker(({ id }) => {
  const todosHandle = Meteor.subscribe('todos.inList', id);
  const loading = !todosHandle.ready();
  const list = Lists.findOne(id);
  const listExists = !loading && !!list;
  return {
    loading,
    list,
    listExists,
    todos: listExists ? list.todos().fetch() : [],
  };
})(ListPage);

I copied it from the guide https://guide.meteor.com/react.html#using-withTracker

2 Likes

check componentDidUpdate lifecycle function of React