A powerful, React hook built with mathjs for safely evaluating, formatting, and analyzing math expressions in React apps.
npm install use-mathjsA React hook built with mathjs for safely evaluating math expressions in your React apps.
---
- Evaluate math expressions (e.g. a + b, 2 * x + 3)
- Validate results and access error state
- Debounce evaluation for performance
- Flexible scope: supports plain objects and Map-like objects (including ES6 Map)
---
``bash`
npm install use-mathjs
> ⚠️ Requires react version 16.8.0 or above (Hooks support).
---
The scope parameter for evaluate/evaluateSync supports:
- Plain objects: { a: 1, b: 2 }get(key: string)
- Map-like objects: Any object implementing , set(key: string, value: unknown), has(key: string), and keys() (including ES6 Map)
This matches the flexibility described in the mathjs documentation.
Type definition:
`typescript
export interface MapLike {
get(key: string): unknown;
set(key: string, value: unknown): unknown;
has(key: string): boolean;
keys(): IterableIterator
}
export type ScopeType = Record
`
Examples:
`tsx`
evaluate('a + b', { a: 1, b: 2 }) // plain object
evaluate('a + b', new Map([['a', 1], ['b', 2]])) // ES6 Map (Map-like)
evaluate('a + b', customMapLike) // any object with get/set/has/keys
---
`tsx
import { useMath } from 'use-mathjs';
function Calculator() {
const { state, evaluate, evaluateSync, reset } = useMath({
initialResult: { result: 0, error: null, isValid: true },
errorMessageMap: (err) => Custom error: ${err instanceof Error ? err.message : String(err)},
debounceMs: 300,
});
return (
---
API
$3
#### Options
-
initialResult — Initial state (default: { result: null, error: null, isValid: null })
- onEvaluated(result) — Callback after evaluation
- errorMessageMap(error) — Custom error message mapping
- debounceMs — Debounce evaluation in milliseconds#### Return
-
state — Current evaluation state { result, error, isValid }
- evaluate(expr, scope?) — Perform evaluation (debounced if set)
- evaluateSync(expr, scope?) — Synchronously evaluate expression
- reset()` — Reset state---
MIT