Best practices with ReactJS and javascript


(Ricardo M Bertani) #1

Hi, I’m new with ReactJS and I would like to know how is the best way to use ReactJS and traditional javascript together. For example, I would like to implement a push to some google analytics events inside my code ReactJS, in this case the most correct would be use the traditional ga tags inside my ReactJS code or to use a ReactJS library as https://github.com/react-ga/react-ga ?

I notice that traditional javascript code sometimes not work when is inside a class that extends ‘Component’…

Best Regards,
Ricardo.


(Clara Attermo) #2

Just add React onclick and put the ga call in the event handler function. If the ga call is not related to a click but should be run as soon as the component is loaded you can put the ga call inside componentDidMount. There’s no extra library needed except for ga. Just make sure ga object is present before calling any method in it (if e g network trouble so ga is never loaded).