dsherret / ts-nameof

nameof in TypeScript
MIT License
492 stars 23 forks source link

React Native : Uncaught ReferenceError: nameof is not defined error at runtime #26

Closed omatrot closed 6 years ago

omatrot commented 6 years ago

I'm trying to use your utility in a ReactXP project. I've followed the instruction to reference the path. It compiles ok, but I have the following error at runtime on web:

Uncaught ReferenceError: nameof is not defined
    at Object.EditExerciseCategoryPropertiesPanel._this._onChangeActivityTag [as onActivityTagChange] (EditExerciseCategoryPropertiesPanel.tsx:120)
    at Object.CategoryPicker._this._onActivityTagValueChange [as onValueChange] (CategoryPicker.tsx:84)
    at Picker._this._onValueChange (Picker.js:32)
    at HTMLUnknownElement.callCallback (react-dom.development.js:1299)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:1338)
    at Object.invokeGuardedCallback (react-dom.development.js:1195)
    at Object.invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:1209)
    at executeDispatch (react-dom.development.js:1432)
    at Object.executeDispatchesInOrder (react-dom.development.js:1454)
    at executeDispatchesAndRelease (react-dom.development.js:1969)
EditExerciseCategoryPropertiesPanel._this._onChangeActivityTag @ EditExerciseCategoryPropertiesPanel.tsx:120
CategoryPicker._this._onActivityTagValueChange @ CategoryPicker.tsx:84
Picker._this._onValueChange @ Picker.js:32
callCallback @ react-dom.development.js:1299
invokeGuardedCallbackDev @ react-dom.development.js:1338
invokeGuardedCallback @ react-dom.development.js:1195
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:1209
executeDispatch @ react-dom.development.js:1432
executeDispatchesInOrder @ react-dom.development.js:1454
executeDispatchesAndRelease @ react-dom.development.js:1969
executeDispatchesAndReleaseTopLevel @ react-dom.development.js:1980
forEachAccumulated @ react-dom.development.js:1946
processEventQueue @ react-dom.development.js:2139
runEventQueueInBatch @ react-dom.development.js:2151
handleTopLevel @ react-dom.development.js:2161
handleTopLevelImpl @ react-dom.development.js:1800
batchedUpdates @ react-dom.development.js:13238
performFiberBatchedUpdates @ react-dom.development.js:1646
stackBatchedUpdates @ react-dom.development.js:1637
batchedUpdates @ react-dom.development.js:1651
batchedUpdatesWithControlledComponents @ react-dom.development.js:1664
dispatchEvent @ react-dom.development.js:1874
react-dom.development.js:1345 Uncaught Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development. See https://fb.me/react-crossorigin-error for more information.
    at Object.invokeGuardedCallbackDev (react-dom.development.js:1345)
    at Object.invokeGuardedCallback (react-dom.development.js:1195)
    at Object.invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:1209)
    at executeDispatch (react-dom.development.js:1432)
    at Object.executeDispatchesInOrder (react-dom.development.js:1454)
    at executeDispatchesAndRelease (react-dom.development.js:1969)
    at executeDispatchesAndReleaseTopLevel (react-dom.development.js:1980)
    at Array.forEach (<anonymous>)
    at forEachAccumulated (react-dom.development.js:1946)
    at Object.processEventQueue (react-dom.development.js:2139)
invokeGuardedCallbackDev @ react-dom.development.js:1345
invokeGuardedCallback @ react-dom.development.js:1195
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:1209
executeDispatch @ react-dom.development.js:1432
executeDispatchesInOrder @ react-dom.development.js:1454
executeDispatchesAndRelease @ react-dom.development.js:1969
executeDispatchesAndReleaseTopLevel @ react-dom.development.js:1980
forEachAccumulated @ react-dom.development.js:1946
processEventQueue @ react-dom.development.js:2139
runEventQueueInBatch @ react-dom.development.js:2151
handleTopLevel @ react-dom.development.js:2161
handleTopLevelImpl @ react-dom.development.js:1800
batchedUpdates @ react-dom.development.js:13238
performFiberBatchedUpdates @ react-dom.development.js:1646
stackBatchedUpdates @ react-dom.development.js:1637
batchedUpdates @ react-dom.development.js:1651
batchedUpdatesWithControlledComponents @ react-dom.development.js:1664
dispatchEvent @ react-dom.development.js:1874

On iPhone:

Can't find variable: nameof
_onChangeActivityTag
EditExerciseCategoryPropertiesPanel.js:73:30

Android:

Can't find variable: nameof
_onChangeActivityTag
    EditExerciseCategoryPropertiesPanel.js:73:30
_onActivityTagValueChange
    CategoryPicker.js:52:48
onValueChange
    Picker.js:38:38
_onChange
    PickerAndroid.android.js:120:33
invokeGuardedCallback
    ReactNativeStack-dev.js:130:19
invokeGuardedCallback
    ReactNativeStack-dev.js:166:43
invokeGuardedCallbackAndCatchFirstError
    ReactNativeStack-dev.js:169:64
executeDispatch
    ReactNativeStack-dev.js:202:128
executeDispatchesInOrder
    ReactNativeStack-dev.js:208:279
executeDispatchesAndRelease
    ReactNativeStack-dev.js:272:58
executeDispatchesAndReleaseTopLevel
    ReactNativeStack-dev.js:276:39
forEachAccumulated
    ReactNativeStack-dev.js:268:65
processEventQueue
    ReactNativeStack-dev.js:340:143
runEventQueueInBatch
    ReactNativeStack-dev.js:637:79
handleTopLevel
    ReactNativeStack-dev.js:642:29
<unknown>
    ReactNativeStack-dev.js:749:51
fiberBatchedUpdates
    ReactNativeStack-dev.js:691:14
performFiberBatchedUpdates
    ReactNativeStack-dev.js:695:31
perform
    ReactNativeStack-dev.js:1382:99
batchedUpdates
    ReactNativeStack-dev.js:2077:139
batchedUpdates$1
    ReactNativeStack-dev.js:1456:61
batchedUpdates
    ReactNativeStack-dev.js:699:31
batchedUpdatesWithControlledComponents
    ReactNativeStack-dev.js:708:30
_receiveRootNodeIDEvent
    ReactNativeStack-dev.js:748:46
receiveEvent
    ReactNativeStack-dev.js:754:56
__callFunction
    MessageQueue.js:306:47
<unknown>
    MessageQueue.js:108:26
__guard
    MessageQueue.js:269:6
callFunctionReturnFlushedQueue
    MessageQueue.js:107:17

Any help appreciated.

dsherret commented 6 years ago

Sorry that this isn't clear and for my long reply. This is because the build hasn't been configured to replace the nameof expressions. Are you using gulp or webpack or something else?

See the setup section on the readme. I'll update that to be at the top of the readme sometime in the future.

omatrot commented 6 years ago

I would say that ReactXP is using webpack because I have a webpack.config.ts file.

dsherret commented 6 years ago

Cool, I don't know enough about webpack, but I believe you should be able to follow the instructions at https://github.com/Kukks/ts-nameof-loader to use this with webpack.

omatrot commented 6 years ago

It works ! To be more precise, for a ReactXP project (And probably a React one too), you have to install the loader as explained in the loader instructions and modify the webpack.config.ts file as below:

Before

module: {
        loaders: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
            { test: /\.tsx?$/, loader: "awesome-typescript-loader" },
        ]
    }

After

module: {
        loaders: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader' and 'ts-nameof-loader'.
            { test: /\.tsx?$/, use: ["awesome-typescript-loader","ts-nameof-loader"] },
        ]
    }
omatrot commented 6 years ago

Sorry, this works only on ReactXP targeting web (using webpack). There is something to do for iOS and Android...

omatrot commented 6 years ago

@dsherret It seems that we need a Babel plugin. Any help appreciated.

omatrot commented 6 years ago

Ok I ended up using your custom setup and it works fine.

dsherret commented 5 years ago

Following up on this... there is a new setup for webpack that allows you to easily inject this library as a custom transformer: https://github.com/dsherret/ts-nameof/blob/master/setup/webpack.md

Unfortunately this library doesn't work with babel. Read more here: https://github.com/dsherret/ts-nameof/issues/37#issuecomment-443548609

ozyman42 commented 5 years ago

Interestingly, I was able to get things working via the ts-nameof-loader, but not with the custom transformer. I may look into this more in the future, but for now I'll just use the loader.