Is it ok to put a method call in ComponentDidMount?

I’m trying to get my twilio sms messages from my twilio account. Trying to fetch the data from Twilio automatically has been a challenge, so I currently hooked up a method that will automatically pull the latest sms when the component mounts. This obviously doesn’t feel or look right since its anti-meteor, anti-pattern. What’s the best way of doing this?

I’m not sure why this would be considered an anti-pattern - from the React componentDidMount docs:

If you want to integrate with other JavaScript frameworks, set timers using setTimeout or setInterval, or send AJAX requests, perform those operations in this method.

Using a Meteor Method falls right into this category (since they’re really just remote procedure calls).

The above being said, if you wanted to move the Method call out, you could consider making the call in a wrapping component (using something like createContainer), then storing the results in a local Collection. That local collection could then be passed into your component as a prop.

4 Likes

Why use a local collection?

It wouldn’t really be necessary - I was just thinking ahead a bit, but it’s likely un-necessary. I was just thinking of a scenario where you might want to move the Method call out somewhere else and have it load a local Collection, then only have your wrapping component work with that local Collection. This way you could control when the Method is called outside of your component (if the call was expensive for example).

Container can have doStuff() which calls the method call
Page uses the passed down doStuff()

Another option is to use a validatedMethod and just pass it down from the container level.

If I don’t have 1 post in the mongoDB componentDidMount does not get my data from an API twilio. I don’t understand why this is.

Is it because I’m wrapping the function with Meteor.wrapAsync?