Comments (16)
I solved it by adding local-storage polyfill for development dependency
npm i -D localstorage-polyfill
OR
yarn add -D localstorage-polyfill
In your App's index.js file add following:
import 'localstorage-polyfill'
This solved error for me.
from react-snapshot.
You should not use localStorage
instance in the component's render
or any method that is evaluated in the server. Move localStorage calls in the componentWillMount method for example. I've solved my problem this way.
from react-snapshot.
@Danjavia, did @arefaslani's strategy work for you? I moved my localStorages
inside componentWillMount
and it still throws the same error, localStorage is not defined
. I also tried to use componentDidMount
.
from react-snapshot.
I have the same trouble too.
from react-snapshot.
:(
from react-snapshot.
I have same problem... How to solve this kind of issues?
from react-snapshot.
Thanks @arefaslani I will to try it.
from react-snapshot.
Jsdom issue won't be fixed, see #44 (comment). If this answers your question please close issue. Thanks
from react-snapshot.
I have the same issue.
As far as I see it the react-snapshot just doesn't have global localStorage, so I'd tried to wrap localStorage and it worked for me.
export const getAuthTokens = () => {
try {
// The problem was here
const tokens = localStorage.getItem(AUTH_ACCESS_TOKEN);
if (tokens !== null) {
return JSON.parse(tokens);
}
return null;
} catch (e) {
console.log(e.message);
return null;
}
};
It's not the best solution, but it helped me.
from react-snapshot.
Did you export this globally? Or inside the component?
from react-snapshot.
Try this with globals :
"globals": {
"window": true,
"browser": true,
"localStorage":true,
}
from react-snapshot.
Use getDerivateStateFromProps, and place the localStorage in there, that worked for me.
from react-snapshot.
I use localstorage in componentDidMount function,not construction,and solved the error
from react-snapshot.
I had the same error, caused by the credentials verification library. I worked this around this by creating a helper function:
export const storage = () => {
if (!typeof window.localStorage === "undefined") return window.localStorage;
else if (!typeof localStorage === "undefined") return localStorage;
else return false;
};
and checking the availability of localStorage before each call to methods using it:
- verifyCredentials(store);
+ storage() && verifyCredentials(store);
from react-snapshot.
I managed to get this working by extending comments earlier and constructing aalocalStorageWraapper.
const localStorageMock = function () {
var store = {};
return {
getItem: function (key) {
return store[key];
},
setItem: function (key, value) {
store[key] = value.toString();
},
clear: function () {
store = {};
},
removeItem: function (key) {
delete store[key];
},
};
};
const storage = () => {
if (!typeof window.localStorage === "undefined") return window.localStorage;
else if (!typeof localStorage === "undefined") return localStorage;
else return localStorageMock();
};
module.exports = { storage };
I had the same error, caused by the credentials verification library. I worked this around this by creating a helper function:
export const storage = () => { if (!typeof window.localStorage === "undefined") return window.localStorage; else if (!typeof localStorage === "undefined") return localStorage; else return false; };
and checking the availability of localStorage before each call to methods using it:
- verifyCredentials(store); + storage() && verifyCredentials(store);
from react-snapshot.
I solved it by adding local-storage polyfill for development dependency
npm i -D localstorage-polyfill
OR
yarn add -D localstorage-polyfill
In your App's index.js file add following:
import 'localstorage-polyfill'
This solved error for me.
I keep getting this error when I import
`TypeError: Cannot set property localStorage of # which has only a getter
(anonymous function)
node_modules/localstorage-polyfill/localStorage.js:39
36 | }
37 | const instance = new LocalStorage()
38 |
39 | global.localStorage = new Proxy(instance, {
40 | set: function (obj, prop, value) {
41 | if (LocalStorage.prototype.hasOwnProperty(prop)) {
42 | instance[prop] = value`
from react-snapshot.
Related Issues (20)
- Starting crawling fail for the query string parameters url
- Snapshot of async re-render (setState) HOT 3
- Error when building when <canvas /> is being used HOT 6
- Can't build with react-snapshot anymore. /build/ doesn't generate .html HOT 4
- Anchor tags with href="javascript;void(0);" will break HOT 1
- Does not prerender anymore with styled-components v3 or v4 HOT 1
- Confusing README HOT 1
- Pass in variables
- Error: Uncaught [Error: Secure random number generation is not supported by this browser. Use Chrome, Firefox or Internet Explorer 11] HOT 1
- TypeScript support HOT 3
- Why got: nginx 404 not found? HOT 1
- "RangeError: Maximum call stack size exceeded" after crawling
- Is the project still maintained?
- Invariant Violation on jsdom while using react-snapshot
- issue with node-fetch: TypeError: Cannot set property 'Headers' of undefined
- Link to Phenomic (https://phenomic.io/) in `README.md` goes to domain expired page.
- npm run build error when using react-snapshot HOT 1
- Can this be used with React Native Web/Expo?
- ‘TypeError: window.matchMedia is not a function’ error HOT 1
- Incompatible with React >18
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-snapshot.