{"record":{"id":"a17815487256c182","repo":"streamich/react-use","slug":"capacity-has-to-be-greater-than-1-got-capacity","errorCode":null,"errorMessage":"Capacity has to be greater than 1, got '${capacity}'","messagePattern":"Capacity has to be greater than 1, got '(.+?)'","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/useStateWithHistory.ts","lineNumber":29,"sourceCode":"  go: (position: number) => void;\n}\n\nexport type UseStateHistoryReturn<S> = [S, Dispatch<IHookStateSetAction<S>>, HistoryState<S>];\n\nexport function useStateWithHistory<S, I extends S>(\n  initialState: IHookStateInitAction<S>,\n  capacity?: number,\n  initialHistory?: I[]\n): UseStateHistoryReturn<S>;\nexport function useStateWithHistory<S = undefined>(): UseStateHistoryReturn<S | undefined>;\n\nexport function useStateWithHistory<S, I extends S>(\n  initialState?: IHookStateInitAction<S>,\n  capacity: number = 10,\n  initialHistory?: I[]\n): UseStateHistoryReturn<S> {\n  if (capacity < 1) {\n    throw new Error(`Capacity has to be greater than 1, got '${capacity}'`);\n  }\n\n  const isFirstMount = useFirstMountState();\n  const [state, innerSetState] = useState<S>(initialState as S);\n  const history = useRef<S[]>((initialHistory ?? []) as S[]);\n  const historyPosition = useRef(0);\n\n  // do the states manipulation only on first mount, no sense to load re-renders with useless calculations\n  if (isFirstMount) {\n    if (history.current.length) {\n      // if last element of history !== initial - push initial to history\n      if (history.current[history.current.length - 1] !== initialState) {\n        history.current.push(initialState as I);\n      }\n\n      // if initial history bigger that capacity - crop the first elements out\n      if (history.current.length > capacity) {\n        history.current = history.current.slice(history.current.length - capacity);","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/streamich/react-use/blob/fbe99c6327e6af94df03bc8bd6ecc5e3ff04fbcc/src/useStateWithHistory.ts#L11-L47","documentation":"Thrown by useStateWithHistory when its `capacity` argument is less than 1. The hook defaults capacity to 10, so this only triggers when a caller explicitly passes 0 or a negative number. Note the message says 'greater than 1' but the actual guard is `capacity < 1`, so a capacity of exactly 1 IS valid — the message wording is slightly inaccurate (it should read 'at least 1' / 'greater than 0').","triggerScenarios":"Calling useStateWithHistory(initialState, capacity, initialHistory) with capacity set to 0 or a negative number, e.g. from a dynamic/config value that underflows. With no argument (default 10) or capacity >= 1, no throw occurs.","commonSituations":"Capacity derived from config/user input that can be 0 (e.g. a 'history length' setting set to 0); arithmetic that subtracts and goes negative; passing undefined through a defaulted-to-0 chain; misunderstanding the off-by-one message and testing the wrong boundary.","solutions":["Pass a capacity of at least 1 (1 is the real minimum): useStateWithHistory(state, Math.max(1, capacity)).","Validate/coerce dynamic values before the call and default to 10 (the library default) when invalid.","If 0 capacity was intended to mean 'no history', use 1 instead, since the hook always tracks at least the current entry.","Treat the error message's 'greater than 1' as imprecise — verify against the code (capacity < 1 throws)."],"exampleFix":"// before\nuseStateWithHistory(state, historyLimit);   // historyLimit may be 0 or negative\nuseStateWithHistory(state, 0);\n\n// after\nuseStateWithHistory(state, Math.max(1, historyLimit));\nuseStateWithHistory(state, 1);  // valid: 1 is allowed","handlingStrategy":"validation","validationCode":"function useStateWithHistorySafe<S, I extends S>(\n  initialState: IHookStateInitAction<S>,\n  capacity: number,\n  initialHistory?: I[]\n) {\n  // Real minimum is 1 (guard is `capacity < 1`), despite the 'greater than 1' message.\n  const safeCapacity = Number.isFinite(capacity) && capacity >= 1 ? capacity : 10;\n  return useStateWithHistory<S, I>(initialState, safeCapacity, initialHistory);\n}","typeGuard":"const isPositiveCapacity = (v: unknown): v is number =>\n  typeof v === 'number' && Number.isFinite(v) && v >= 1;\n\nconst cap = isPositiveCapacity(rawCapacity) ? rawCapacity : 10;\nconst [state, setState, history] = useStateWithHistory(initial, cap);","tryCatchPattern":"// The throw occurs on first render inside the hook; validate capacity before\n// calling instead of try/catching the hook invocation.","preventionTips":["Clamp dynamic capacity with Math.max(1, capacity) before passing.","Default to the library's 10 when a config value is missing or non-positive.","Remember the message is off-by-one: 1 is valid, only 0/negatives throw.","Treat a configured 0 as 'use the default', not as a real capacity."],"tags":["react","hooks","validation","history","config"],"backgroundTag":null,"analyzedSha":"fbe99c6327e6af94df03bc8bd6ecc5e3ff04fbcc","analyzedAt":"2026-08-12T19:24:06.802Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}