Skip to content

When using AsyncLocalStorage, "Illegal invocation" error occurs #18

Closed
@Venryx

Description

@Venryx

My config is pretty simple:

persist('some', storeM, {
	blacklist: [],
}).then(() => { ... });

Yet I get this error:

VM1685:1 Uncaught TypeError: Illegal invocation
    at eval (eval at callWithPromise (mst-persist.esm.js:42), <anonymous>:1:1)
    at callWithPromise (mst-persist.esm.js:42)
    at Object.getItem (mst-persist.esm.js:26)
    at persist (mst-persist.esm.js:89)
    at RootUIWrapper.ComponentWillMount (Root.js:198)
    at RootUIWrapper.UNSAFE_componentWillMount (index.js:694)
    at callComponentWillMount (react-dom.development.js:13371)
    at mountClassInstance (react-dom.development.js:13461)
    at updateClassComponent (react-dom.development.js:16986)
    at beginWork$1 (react-dom.development.js:18505)

Googled it, and it appears to be the same issue here: https://stackoverflow.com/questions/41126149/using-a-shortcut-function-gives-me-a-illegal-invocation-error

To fix, you can either bind the getItem/setItem functions to the localStorage object, or do the equivalent by using a (...args)=>localStorage.XXX(...args) wrapper function.

For now I am fixing it by globally applying the bind:

window.localStorage.getItem = window.localStorage.getItem.bind(window.localStorage);
window.localStorage.setItem = window.localStorage.setItem.bind(window.localStorage);

However, the library should not require the developer to apply this global binding.

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: bugSomething isn't working properly or is fixedsolution: workaround availableThere is a workaround available for this

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions