cond(predicate): PartialLens
ts
cond: <A>(predicate: (a: A) => boolean) => PartialLens<A, A>;
ts
cond: <A>(predicate: (a: A) => boolean) => PartialLens<A, A>;
This combinators takes a predicate and, when passed to derive
, returns a new partial optic focused either on the original value or undefined if the predicate returns false.
Example:
ts
import {cond } from "@optics/react/combinators";constnumberOptic =createState (42);constevenNumberOptic =numberOptic .derive (cond ((n ) =>n % 2 === 0));evenNumberOptic .get (); // 42evenNumberOptic .set ((prev ) =>prev + 1);evenNumberOptic .get (); // undefinednumberOptic .get (); // 43
ts
import {cond } from "@optics/react/combinators";constnumberOptic =createState (42);constevenNumberOptic =numberOptic .derive (cond ((n ) =>n % 2 === 0));evenNumberOptic .get (); // 42evenNumberOptic .set ((prev ) =>prev + 1);evenNumberOptic .get (); // undefinednumberOptic .get (); // 43