Moccedelic
Moccedelic

Reputation: 1

Get last state of a derived store in svelte

Is it possible to get the last state of a store that is derived from a custom store in svelte?

export const derivedState(customStore, (derived) => {
   // what i would like to do here is something like
   if (derived.status === 'OK'){
      // do something and with derived and return
   } else {
      // return whatever the last value of derivedState was
   }  
}

Upvotes: 0

Views: 751

Answers (1)

dummdidumm
dummdidumm

Reputation: 5426

Store the last state outside and update when appropriate

let lastState;
export const derivedState(customStore, (derived) => {
   if (derived.status === 'OK'){
      lastState = computeNewStateSomehow(derived);
   }
   return lastState; 
}

Upvotes: 1

Related Questions