Skip to content

previous

import { previous } from 'patronum';
// or
import { previous } from 'patronum/previous';

Motivation

The method allows to get previous value of given store. Usually need for analytics

Formulae

$target = previous($source);
$target = previous($source, 'initial value');

Arguments

  1. $source ([Store]) - source store
  2. defaultValue (optional) - default value for $target store, if not passed, null will be used

Returns

  • $target ([Store]) - new store that contain previous value of $source after first update and null or default value (if passed) before that

Example

Push analytics with route transition:

import { createStore, createEvent, createEffect, sample } from 'effector';
import { previous } from 'patronum';
const openNewRoute = createEvent<string>();
const $currentRoute = createStore('main_page');
const $previousRoute = previous($currentRoute);
const sendRouteTransitionFx = createEffect(async ({ prevRoute, nextRoute }) => {
console.log(prevRoute, '->', newRoute)
await fetch(...)
});
sample({clock: openNewRoute, target: $currentRoute});
sample({
clock: openNewRoute,
source: {
prevRoute: $previousRoute,
nextRoute: $currentRoute,
},
target: sendRouteTransitionFx,
});
openNewRoute('messages');
// main_page -> messages
openNewRoute('chats');
// messages -> chats