Skip to main content
Module

x/modify_via_query/mod.ts>modify

Natural and type-safe query to mutate a copy of data without changing the original source
Latest
function modify
import { modify } from "https://deno.land/x/modify_via_query@1.0.7/mod.ts";

Variant 1:

State => Update => State

Example:

modify(state)(state => state.count.$set(1))

Can be curried, for example:

const m = modify({count: 0})
const add = m(state => state.count.$apply(x => x + 1))
const minus = m(state => state.count.$apply(x => x - 1))

Returns

(update: (state: Modifiable<T, T>) => Modifiable<T, T>) => T

Variant 2:

Update => State => State

Example:

modify(state => state.count.$set(1))(state)

This variant is useful when used with function that takes a function with the type of State -> State. Example of such function is setState or this.setState of React.
Usage example:

const [state, setState] = React.useState({count: 0})
const add = () => setState(
 modify(state => state.count.$apply(x => x + 1))
)
const minus = () => setState(
 modify(state => state.count.$apply(x => x - 1))
)

Parameters

update: (state: Modifiable<T, T>) => Modifiable<T, T>

Returns

(state: T) => T