Build and evaluate JsonLogic rules with React components.
react-json-logic is headless. No CSS is shipped. You style it however you want.
pnpm add react-json-logic react react-domimport { useState } from "react";
import JsonLogicBuilder, { applyLogic, type JsonLogicValue } from "react-json-logic";
function Example() {
const [rule, setRule] = useState<JsonLogicValue>("");
const data = { user: { age: 21 } };
return (
<>
<JsonLogicBuilder value={rule} data={data} onChange={setRule} />
<p>Result: {String(applyLogic(rule, data))}</p>
</>
);
}- Default export:
JsonLogicBuilder - Named exports:
applyLogic,rule,validate,OPERATORS,FIELD_TYPES - Core types:
JsonLogicValue,JsonLogicData,ValidationResult,ValidationError
value: JsonLogicValue- controlled current ruleonChange: (value: JsonLogicValue) => void- called whenever the rule changesdata?: JsonLogicData | string- sample data for accessor suggestions (var)onDataError?: (err: unknown, raw: string) => void- parse error hook whendatais a string
Use stable data-rjl-* attributes to style the rendered DOM (for example with Tailwind, CSS Modules, or vanilla CSS).
Common hooks include:
data-rjl-builderdata-rjl-anydata-rjl-fielddata-rjl-adddata-rjl-removedata-rjl-operator-triggerdata-rjl-operator-popupdata-rjl-input-valuedata-rjl-accessor-inputdata-rjl-higher-order
Use the typed rule factory to construct rules without hand-writing JSON:
import { applyLogic, rule, validate } from "react-json-logic";
const r = rule.and(rule.eq(rule.var("user.age"), 21), rule.gt(rule.var("score"), 100));
applyLogic(r, { user: { age: 21 }, score: 150 }); // true
validate(r); // { ok: true }This repository is a workspace with:
packages/react-json-logic(publishable npm package)apps/example(demo app)
From the workspace root:
vp install
pnpm verify
pnpm dev:exampleSee CONTRIBUTING.md.
See SECURITY.md.