Skip to content

Fix persisted single-value stores#4859

Open
joshhanley wants to merge 1 commit into
mainfrom
josh/fix-direct-persist-store
Open

Fix persisted single-value stores#4859
joshhanley wants to merge 1 commit into
mainfrom
josh/fix-direct-persist-store

Conversation

@joshhanley

Copy link
Copy Markdown
Collaborator

The Scenario

When Alpine.$persist() is used as the direct value of a single-value store, the store initially renders [object Object] instead of its persisted value.

Alpine.store('language', Alpine.$persist('EN').as('language'))
<button
    x-data
    x-on:click="$store.language = $store.language === 'EN' ? 'IT' : 'EN'"
    x-text="$store.language"
></button>

The Problem

Alpine.$persist() wraps the initial value until Alpine sets up persistence.

Alpine currently initialises wrapped values found inside a store object, but skips the wrapper when it is the entire store.

The wrapper is therefore exposed through $store.language, causing it to display [object Object]. Changes to the store are also not saved.

The Solution

The solution is to check whether the store value itself needs to be initialised.

When it does, Alpine replaces it with the real persisted value and watches that store for future changes. Object stores continue using the existing logic for persisted properties.

Fixes #4855

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Alpine.$persist() used as direct store displays [Object Object] before hydration

1 participant