unstated-persist icon indicating copy to clipboard operation
unstated-persist copied to clipboard

Cannot use dynamic persist key

Open mlajtos opened this issue 7 years ago • 1 comments

If you want to use dynamic persist key, you are out of luck:

class Mu extends PersistContainer {
    persist = {
        key: "defaultKey",
        version: 1,
        storage: ...,
    }
    constructor(key) {
        super()
        this.persist.key = key  // too late, container is already rehydrated
    }
}

Hacky solution to this problem is to wrap it:

const Mu = (key) => new (class extends PersistContainer {
    persist = {
        key,
        version: 1,
        storage: ...,
    }
}) ()

mlajtos avatar Oct 21 '18 20:10 mlajtos

Does following work for you?

class Mu extends PersistContainer {
    constructor(key) {
        super()

 		this.persist = {
        	key,
        	version: 1,
        	storage: ...,
    	}
    }
}

JPeer264 avatar Oct 24 '18 09:10 JPeer264