React Query Builder Component
React Query Builder is a highly customizable React component designed for constructing complex queries and filters dynamically. It currently maintains a stable version of 8.15.0, with a release cadence that includes frequent patch releases for bug fixes and security updates, alongside regular minor version updates introducing new features and compatibility improvements. Key differentiators include its extensive customization options, first-class support for integration with popular UI libraries like Ant Design, Bootstrap, Chakra UI, and Fluent UI via dedicated compatibility packages, and a comprehensive suite of utility functions for importing and exporting queries to various formats, including SQL, MongoDB, and JSON. The library focuses on providing a flexible and robust solution for building rule-based query interfaces in React applications.
Common errors
-
unknown top level operator: $not
cause Using an older version of react-querybuilder or MongoDB driver that doesn't correctly handle `$not` for negated rule groups in MongoDB queries.fixUpgrade `react-querybuilder` to version 8.14.4 or higher. The library now correctly uses `$nor` for rule group negation in MongoDB export formats. -
TypeError: Cannot read properties of undefined (reading 'Provider')
cause Missing `react-dnd` or `react-dnd-html5-backend` when using `@react-querybuilder/dnd` package for drag-and-drop functionality.fixEnsure `react-dnd` and its necessary backend (e.g., `react-dnd-html5-backend`) are installed as `react-dnd` became an optional peer dependency in `v8.15.0` for `@react-querybuilder/dnd`. -
Type 'typeof import("react-dnd")' is not assignable to type 'Partial<ReactDnD.DndProviderProps>'cause Attempting to pass raw `react-dnd` exports directly to the `dnd` prop of `QueryBuilder` instead of using the adapter.fixUse the `createReactDnDAdapter` function from `@react-querybuilder/dnd` to properly wrap `react-dnd` exports. Example: `dnd={createReactDnDAdapter()}`.
Warnings
- deprecated Directly passing raw `react-dnd` exports as the `dnd` prop is deprecated. Although it still works and is auto-detected, the preferred method is to use `createReactDnDAdapter`.
- deprecated The `useReactDnD` hook in `@react-querybuilder/dnd` is deprecated.
- gotcha The `react-dnd` package is now an *optional* peer dependency for `@react-querybuilder/dnd`. If you use the `dnd` package, ensure `react-dnd` is installed in your project.
- breaking MongoDB export formats (`"mongodb_query"`/`"mongodb"`) for rule group negation now use `$nor` instead of `$not`. This fixes a runtime error but changes the generated query structure for negated groups.
- gotcha `transformQuery` and `merge[Any]Translation[s]` now include guards against prototype pollution. While this is a security fix, it might subtly change behavior if an application was inadvertently relying on or being affected by such vulnerabilities.
Install
-
npm install react-querybuilder -
yarn add react-querybuilder -
pnpm add react-querybuilder
Imports
- QueryBuilder
const QueryBuilder = require('react-querybuilder').QueryBuilder;import { QueryBuilder } from 'react-querybuilder'; - RuleGroupType
import { RuleGroupType } from 'react-querybuilder';import type { RuleGroupType } from 'react-querybuilder'; - Field
import { Field } from 'react-querybuilder';import type { Field } from 'react-querybuilder'; - createReactDnDAdapter
import { createReactDnDAdapter } from '@react-querybuilder/dnd';
Quickstart
import { useState } from 'react';
import { Field, QueryBuilder, RuleGroupType } from 'react-querybuilder';
import 'react-querybuilder/dist/query-builder.css';
const fields: Field[] = [
{ name: 'firstName', label: 'First Name', placeholder: 'e.g. John' },
{ name: 'lastName', label: 'Last Name', placeholder: 'e.g. Doe' },
{ name: 'age', label: 'Age', inputType: 'number', placeholder: 'e.g. 30' },
{ name: 'address', label: 'Address', placeholder: 'e.g. 123 Main St' },
{ name: 'phone', label: 'Phone', placeholder: 'e.g. 555-1234' },
{ name: 'email', label: 'Email', validator: ({ value }) => /^[^@]+@[^@]+/.test(value), placeholder: 'e.g. user@example.com' },
{ name: 'isDev', label: 'Is a Developer?', valueEditorType: 'checkbox', defaultValue: false },
];
const initialQuery: RuleGroupType = {
combinator: 'and',
rules: [
{ field: 'age', operator: '>', value: '25' },
{ field: 'isDev', operator: '=', value: true }
],
};
export const App = () => {
const [query, setQuery] = useState(initialQuery);
return (
<div>
<h2>React Query Builder Example</h2>
<QueryBuilder
fields={fields}
defaultQuery={query}
onQueryChange={setQuery}
showCombineatorsOnLoad={true}
/>
<div style={{ marginTop: '20px' }}>
<h3>Current Query State:</h3>
<pre>{JSON.stringify(query, null, 2)}</pre>
</div>
</div>
);
};