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-domRequires React 19 (react / react-dom peer ^19.0.0).
import { 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:
JsonLogicBuilderProps,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). The full attribute table is in the package README.
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)
Setup, workflow, and release notes: CONTRIBUTING.md.
See SECURITY.md.
