Skip to content
Justice Tech School

Derived Values

Do not store what you can calculate. Compute totals, filtered lists and flags during render from existing state.

Example
const items = [{ p: 5, q: 2 }, { p: 3, q: 1 }];
const total = items.reduce((s, i) => s + i.p * i.q, 0);
const count = items.length;
console.log(total, count);

Result

13 2

Exercise

Calculate a sum.

items.((s, i) => s + i.p, 0)

Quiz Test

Should totals be stored in state?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.