Alpine $persist returns [object] instead of storage value in Alpine.store #4248
Answered
by
SimoTod
arthurshlain
asked this question in
1. Help
-
I'm trying to use the Alpine.$persist global, but got object instead of string: The value with specified key accessibility.js import Alpine from "alpinejs"
import persist from "@alpinejs/persist"
Alpine.plugin(persist)
Alpine.store('accessibility', {
activeFontSizeClassName: Alpine.$persist('text-[16px]').as('accessibility_fontSizeClassName'),
init() {
console.log(this.activeFontSizeClassName);
// Proxy(Object) {initialValue: 'text-[16px]', _x_interceptor: true, …}
document.documentElement.classList.add(this.activeFontSizeClassName);
// ↑ (string expected)
// Uncaught DOMException: Failed to execute 'add' on 'DOMTokenList':
// The token provided ('[object Object]') contains HTML space characters,
// which are not valid in tokens.
},
}); app.js import Alpine from 'alpinejs'
// … import other Alpine plugins
import './store/accessibility'
// … import data objects
window.Alpine = Alpine
Alpine.start()
|
Beta Was this translation helpful? Give feedback.
Answered by
SimoTod
Jun 1, 2024
Replies: 2 comments 6 replies
-
init() {
Alpine.nextTick(() => {
console.log(this.activeFontSizeClassName);
document.documentElement.classList.add(this.activeFontSizeClassName);
})
} |
Beta Was this translation helpful? Give feedback.
6 replies
Answer selected by
arthurshlain
-
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
init
runs too early when persist is not the persisted value yet. You need to use nextTick