GithubHelp home page GithubHelp logo

Comments (16)

patelrikin avatar patelrikin commented on June 18, 2024 7

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.

arefaslani avatar arefaslani commented on June 18, 2024 5

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.

felipedaraujo avatar felipedaraujo commented on June 18, 2024 1

@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.

Danjavia avatar Danjavia commented on June 18, 2024

I have the same trouble too.

from react-snapshot.

Danjavia avatar Danjavia commented on June 18, 2024

:(

from react-snapshot.

arefaslani avatar arefaslani commented on June 18, 2024

I have same problem... How to solve this kind of issues?

from react-snapshot.

Danjavia avatar Danjavia commented on June 18, 2024

Thanks @arefaslani I will to try it.

from react-snapshot.

stereobooster avatar stereobooster commented on June 18, 2024

Jsdom issue won't be fixed, see #44 (comment). If this answers your question please close issue. Thanks

from react-snapshot.

kostyaVyrodov avatar kostyaVyrodov commented on June 18, 2024

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.

Devilla avatar Devilla commented on June 18, 2024

Did you export this globally? Or inside the component?

from react-snapshot.

Devilla avatar Devilla commented on June 18, 2024

Try this with globals :

"globals": {
"window": true,
"browser": true,
"localStorage":true,
}

from react-snapshot.

cesargamboa avatar cesargamboa commented on June 18, 2024

Use getDerivateStateFromProps, and place the localStorage in there, that worked for me.

from react-snapshot.

yuyuanlin1991 avatar yuyuanlin1991 commented on June 18, 2024

I use localstorage in componentDidMount function,not construction,and solved the error

from react-snapshot.

DonGiulio avatar DonGiulio commented on June 18, 2024

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.

squeakycheese75 avatar squeakycheese75 commented on June 18, 2024

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.

oseisaac avatar oseisaac commented on June 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.