reset
import { reset } from 'patronum';// orimport { reset } from 'patronum/reset';reset({ clock, target })
Motivation
The method allow to reset many stores by a single line
Formulae
reset({ clock, target });- When
clockis triggered, reset store/stores intargetto the initial value.
Arguments
clock: Unit<any> | Array<Unit<any>>— Any kind of units is accepted (Store, Event, Effect).target: Store<any> | Array<Store<any>>— Each of these stores will be reset to the initial values whenclockis happened.
Example
import { createEvent, createStore } from 'effector';import { reset } from 'patronum/reset';
const pageUnmounted = createEvent();const userSessionFinished = createEvent();
const $post = createStore(null);const $comments = createStore([]);const $draftComment = createStore('');
reset({ clock: [pageUnmounted, userSessionFinished], target: [$post, $comments, $draftComment],});import { createStore } from 'effector';import { reset } from 'patronum/reset';
const $post = createStore(null);const $comments = createStore([]);const $draftComment = createStore('');
const resetEvent = reset({ target: [$post, $comments, $draftComment] });Alternative
First variant is writing each reset by yourself:
$post.reset([pageUnmounted, userSessionFinished]);$comments.reset([pageUnmounted, userSessionFinished]);$draftComment.reset([pageUnmounted, userSessionFinished]);There has another way — use domain:
import { createDomain, createStore } from 'effector';const resetOnSomeCases = createDomain();
const $post = resetOnSomeCases.createStore(null);const $comments = resetOnSomeCases.createStore([]);const $draftComment = resetOnSomeCases.createStore('');
resetOnSomeCases.onCreateStore((store) => { store.reset([pageUnmounted, userSessionFinished]);});reset({ target })
Motivation
The method allow to reset many stores by a single line with no clock passing
Formulae
const resetEvent = reset({ target });- When
resetEventis triggered, reset store/stores intargetto the initial value.
Arguments
target: Store<any> | Array<Store<any>>— Each of these stores will be reset to the initial values whenresetEventis triggered.
Returns
resetEvent(Event<void>)— New event that reset store/stores intarget.
Example
import { createEvent, createStore } from 'effector';import { reset } from 'patronum/reset';
const $post = createStore(null);const $comments = createStore([]);const $draftComment = createStore('');
const resetEvent = reset({ target: [$post, $comments, $draftComment] });Alternative
Write reset event by yourself:
import { createEvent, createStore } from 'effector';import { reset } from 'patronum/reset';
const $post = createStore(null);const $comments = createStore([]);const $draftComment = createStore('');
const resetEvent = createEvent();
reset({ clock: resetEvent, target: [$post, $comments, $draftComment],});