Problem to install Mixpanel SDK - React Native

1.4k Views Asked by At

I need to configure the mixpanel in my work and for that I created a test project to understand how to configure the mixpanel SDK.

I followed the basic examples in the documentation. https://github.com/davodesign84/react-native-mixpanel

But I'm having a problem: 'null is not an object (evaluating RNMixpnael.sharedInstanceWithToken)'.

this is my code:

import React from 'react';
import {
  SafeAreaView,
  Text,
  StatusBar,
} from 'react-native';

import Mixpanel from 'react-native-mixpanel';

Mixpanel.sharedInstanceWithToken("MY_PROJECT_TOKEN");

const App: () => React$Node = () => {
  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
       <Text>Testing Mixpanel SDK</Text>
      </SafeAreaView>
    </>
  );
};

export default App;

And the error:

enter image description here

Could someone help me understand what I'm doing wrong?

2

There are 2 best solutions below

0
On

What worked for me was adding this line to MainApplication.java

import com.kevinejohn.RNMixpanel.RNMixpanel;
2
On

In react-native versions newer than 0.60 auto-linking should work. All you have to do is:

  1. install react-native-mixpanel using npm or yarn

  2. add pod 'Mixpanel' to your Podfile, initialize it with the token

    More info here: https://mixpanel.com/help/reference/ios

  3. run pod install inside ios folder <-- usually missing this or not restarting the JS server is the cause

  4. import it in your RN codd: import Mixpanel from 'react-native-mixpanel'; as you did

If the null error still shows up just make sure JS server is stopped, remove Xcode derived data, clean cache (npx watchman watch-del-all && yarn start --reset-cache) and rebuild. It should work.