neciu/react-mixpanel

Broken in React v15.5.0+

seenickcode opened this issue · 6 comments

React doesn't use Prop types anymore by default. Running this example throws an error:
react 'PropTypes' is not defined no-undef

Adding the new prop-types packages still doesn't work:

App.propTypes = {
  mixpanel: PropTypes.string.isRequired,
};

Throws: "TypeError: Cannot read property 'track' of undefined" when using it in your example like this:

this.context.mixpanel.track('myeventname');

It looks like the ugly alternative for now is to just use the mixpanel-browser package. Holy crap, yuck, is there a better way?

import mixpanel from 'mixpanel-browser';

class MyComponent extends Component {

  render() {
      mixpanel.init('mykey');
      if (process.env.NODE_ENV === 'production') {
        mixpanel.track('myevent');
      };
    
      // ...
  }
}
neciu commented

Thanks for that. I'll fix that in a day or so.

neciu commented

PropTypes issue was resolved some time ago with version 0.0.9.

I believe you have a typo:
Instead of:

App.propTypes = {
  mixpanel: PropTypes.string.isRequired,
};

You should have:

App.contextTypes = {
    mixpanel: PropTypes.object.isRequired
};

Here is full example app file: https://github.com/neciu/react-mixpanel/blob/master/examples/simple/src/app.js

More about context you can find here.

I have double checked the example and it works fine. Looking forward for your response.

Hey @neciu thanks for the quick reply.

Unfortunately, I get the same error, with your code as well.

Maybe it could be an eslint config issue? I found this article re that https://stackoverflow.com/questions/45692537/proptypes-is-not-defined

neciu commented

Yup it seems to be problem with create-react-app/eslint.

Please provide minimal working example of the error so I can help further!

neciu commented

Since I released the new version of the module, that requires React >= 16.3 - I'm closing the issue.