3

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')

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' });
  }
}

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

Emulator error screenshot:

emulator error action.type undefined

Matthew
  • 2,035
  • 4
  • 25
  • 48

1 Answers1

1

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.

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.

LemonPie
  • 806
  • 1
  • 11
  • 23