In Python I can locally import the headers on the fly. Right now I am working on the Reactjs and localStorage which is normally run on client. Now it has running in the server side. The problem has been address here
But I am accidentally commit the mock class to source code. So I started to use process.env in order to do local import like Python
package.json
{
...,
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
I know how to use process.env from this. But I still can not get out from the issue. I got 'import' and 'export' may only appear at the top level
export const getAuthToken = () => {
if (process.env === 'development'){
import localStorage from './localStorageMock';
}
return localStorage.getItem('authToken');
};
Question:
How to do locally import like Python in Javascript?
If not how can I solve this issue?
Attempt1:
localStorageMock2.js. I try another syntax.
function LocalStorageMock(){
this.store = {};
this.getItem = function(key){
return this.store[key] || null
};
this.setItem = function(key, value) {
this.store[key] = value
};
this.removeItem = function(key) {
delete this.store[key]
};
}
const localStorageMock = new LocalStorageMock(); export default localStorageMock;
Trackback1:
When I look at the cache object
console.log(cache);
{ default:
LocalStorageMock {
store: {},
getItem: [Function],
setItem: [Function],
removeItem: [Function] } }
I think I see removeItem function. But no luck.
console.log(typeof cache.removeItem);
undefined
Then like I expected. It raises the exception.
return cache.removeItem('authToken');
console.error node_modules/redux-saga/lib/internal/utils.js:240
uncaught at askBackend TypeError: cache.removeItem is not a function
at Object.<anonymous>.exports.removeAuthToken (/Users/sarit/study/HT6MInterface/f1/src/utils.js:50:16)
at Object.<anonymous>.exports.VerifyTokenReducer (/Users/sarit/study/HT6MInterface/f1/src/containers/reducers.js:20:34)