React-Native Android操作错误:'undefined不是对象(评估'action.type')' [英] React-Native Android actions error: 'undefined is not an object (evaluating 'action.type')'

查看:118
本文介绍了React-Native Android操作错误:'undefined不是对象(评估'action.type')'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用React Native v.43.0-rc.3,当我点击我的切换按钮时,我的Android模拟器出现此错误:

i'm using React Native v.43.0-rc.3 and have this error in my Android emulator when i hit my toggle button:

undefined is not an object (evaluating 'action.type')

我的切换按钮连接到 /action/index.js 中的函数:

My toggle button is connected to a function in /action/index.js:

export const toggleUpdateSwitch = (showUpdates) => {
  // error goes away when i remove this line below ----+v
  AnalyticsTracker.trackEvent({"category": "profile", "action" : "toggle", "label": "section/updates"})

  async dispatch => {
    ToastAndroid.show(TOGGLE_UPDATES_NO_DATA_MESSAGE, ToastAndroid.LONG, ToastAndroid.CENTER);
    const response = await fetch('http://my.url.com/doSomething', { headers, method: 'GET' });
  }
}

所以我添加了上面的行(AnalyticsTracker),这是我切换时导致错误的原因。有什么想法?

So i added the line above (AnalyticsTracker), and that's what causes the error when i toggle. Any ideas?

模拟器错误截图:

推荐答案

之前我遇到过这样的问题。您必须将跟踪器代码放在异步调度中。根据我的经验,每当我在函数中进行异步调度时,如果我在其外部有同步代码,我总是会收到此错误。我不清楚为什么会发生这种情况,但我知道这对我来说是固定的。

I had an issue like this before. You have to put the tracker code inside your async dispatch. From my experience, whenever I have async dispatch in my functions, I always get this error if I have synchronous code outside of it. I don't have a clear idea of why this is happening, but I know that fixed it for me.

我的理论是当你做异步时,你是开始一个不同的线程。通过在其外部使用同步代码,主线程上发生了一些事情,但它没有返回任何内容。因此,当你执行action.type时,你得到undefined不是一个对象,因为默认情况下我认为RN期望一个具有type属性的对象被返回,但是你没有返回任何东西。

My theory is that when you do an async, you are starting a different thread. By having synchronous code outside of it, there is something happening on the main thread but it is not returning anything. Hence you get undefined is not an object when you do action.type because the by default I think RN expects an object with a type attribute to be returned back, but you aren't returning anything back.

这篇关于React-Native Android操作错误:'undefined不是对象(评估'action.type')'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆