reduceable

A helpful utility to use a sveltejs store as a reduceable store. It's like a useReducer hook in React. Define a reducer function and an initial state and you can use a regular `$` syntax to subscribe to state updates and a `dispatch` method to dispatch actions.

Demo

0

Usage

<script lang="ts">
import { reduceable } from "svelte-legos";

function reducer(state: number, action: string) {
	switch (action) {
		case "inc":
			return state + 1;
		case "dec":
			return state - 1;
		default:
			return state;
	}
}

const counter = reduceable(reducer, 0);
</script>

<p>{$counter}</p>
<button on:click={() => counter.dispatch("inc")}>+</button>
<button on:click={() => counter.dispatch("dec")}>-</button>