TypeError: Attempted to assign to readonly property - when changing a global var from a reducer function

Viewed 454

I'm new to Javascript/React Native and I can't wrap my head around this error.

var path = []; // Global var

export const updatePathReducer = (
  state = path,
  action
) => {
  switch (action.type) {
    case "UPDATEPATH":

      console.log("action.currentposition" + action.currentPosition.latitude); // The value that gets printed here is not null

      path.push(action.currentPosition); // <---- TypeError: Attempted to assign to readonly property 

      return path;

    default:
      return state;
  }
};
TypeError: Attempted to assign to readonly property.
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:171:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/expo-error-recovery/build/ErrorRecovery.fx.js:12:21 in ErrorUtils.setGlobalHandler$argument_0
at [native code]:null in flushedQueue
at [native code]:null in invokeCallbackAndReturnFlushedQueue

enter image description here

From what I've learned I should be able to just change a global variable like this. I hope someone can explain what I'm doing wrong. Thanks!

0 Answers
Related