Concepts

Pure action reducer

Try the free JavaScript compiler

Compute the next application state from explicit action data without effects or prior-state mutation.

Formal Syntax & Structure

javascript syntaxjavascript
reduce(state, action) → nextState

Working Example

Example Snippetjavascript
(async () => {const reduce=(s,a)=>a.type==="ADD"?{...s,ids:[...s.ids,a.id]}:a.type==="CLEAR"?{...s,ids:[]}:s;const old={ids:["A"]};const next=reduce(old,{type:"ADD",id:"B"});console.log(JSON.stringify(old.ids),JSON.stringify(next.ids),reduce(next,{type:"OTHER"})===next);})()
JavaScript Reference