Nicholas Singh
Nicholas Singh

Reputation: 101

How do i update a boolean value with svelte custom stores?

import { writable } from 'svelte/store';

const createStore = () => {
    const { update, subscribe } = writable({
        url: 'hello',
        amount: 42,
        tip: 12,
        payment: false,
    });

    return {
        subscribe,
        update,
        clicker: () => {
            count.payment.update((count.payment = true));
        },
    };

};

export const count = createStore();

Upvotes: 0

Views: 2304

Answers (1)

Stephane Vanraes
Stephane Vanraes

Reputation: 16451

two problems:

  • count is not defined that clicker function
  • that is not how update works

update takes as argument a function that has as argument the current value and as return value the new value: store.update(oldValue => newValue)

With those two things in mind, the correct code would be:

return {
  clicker: () => update(current => {
   current.payment = true;
   return current;
  })
}

Upvotes: 3

Related Questions