在React Native中,Mixpanel.sharedInstanceWithToken不是一个函数。

3
我想将Mixpanel与React Native集成,我使用了这个github源代码https://github.com/davodesign84/react-native-mixpanel
我尝试了所有描述的相同步骤,但是出现了以下消息。
Mixpanel.sharedInstanceWithToken is not a function

'Mixpanel.sharedInstanceWithToken'未定义

    var Mixpanel = require('react-native-mixpanel');

     componentDidMount() {
        console.log('============>', Mixpanel) // it returns object 
        Mixpanel.sharedInstanceWithToken('xxx');
    }

enter image description here

我尝试了很多次,但都没有成功。应用程序启动成功,但会停留在屏幕上,我使用了Mixpanel.sharedInstanceWithToken('xxx')。
谢谢。

1
尝试使用 Mixpanel.default.sharedInstanceWithToken 或将 require 更改为 import Mixpanel from 'react-native-mixpanel' - savelichalex
谢谢,但现在我遇到了这个错误:“Mixpanel实例尚未初始化,请运行initialize()并等待其承诺解决后再调用track(...)”,但是没有这样的函数。 - Nomura Nori
1
我刚刚浏览了这个库的代码。你不需要自己调用initialize(),当你调用sharedInstanceWithToken时它已经为你完成了,但你需要注意这个函数返回一个Promise,所以在调用其他方法之前,你需要使用then,例如: Mixpanel.default.sharedInstanceWithToken(MY_TOKEN).then(() => { // 在这里使用其他方法 }) - savelichalex
2个回答

1

正如@savelichalex在评论中提到的,使用这个方法是有效的:

const Mixpanel = require('react-native-mixpanel');
Mixpanel.default.sharedInstanceWithToken('MY_TOKEN');

0

这对我很有帮助。 我在__mocks__文件夹中创建了react-native-mixpanel.js文件。 然后把以下内容放到该文件中:

export default {
  sharedInstanceWithToken: jest.fn(),
};


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接