Skip to main content

values(): Lens

ts
values: <A>() => Lens<A[], Record<string, A>>;
ts
values: <A>() => Lens<A[], Record<string, A>>;

This combinator, when passed to derive, returns an optic focused on the values of the focused record. It is useful to map over a the values of a record.

Example:

ts
import { values } from "@optics/react/combinators";
 
const capitalsOptic = createState<Record<string, string>>({
France: "paris",
Italy: "roma",
Australia: "canberra",
});
 
const valuesOptic = capitalsOptic.derive(values());
const valuesOptic: Optic<string[], {}>
 
valuesOptic.get(); // ['paris', 'roma', 'canberra'];
 
valuesOptic.map().set((prev) => prev[0].toUpperCase() + prev.slice(1));
 
capitalsOptic.get(); // { France: "Paris", Italy: "Roma", Australia: "Canberra" }
ts
import { values } from "@optics/react/combinators";
 
const capitalsOptic = createState<Record<string, string>>({
France: "paris",
Italy: "roma",
Australia: "canberra",
});
 
const valuesOptic = capitalsOptic.derive(values());
const valuesOptic: Optic<string[], {}>
 
valuesOptic.get(); // ['paris', 'roma', 'canberra'];
 
valuesOptic.map().set((prev) => prev[0].toUpperCase() + prev.slice(1));
 
capitalsOptic.get(); // { France: "Paris", Italy: "Roma", Australia: "Canberra" }