React Query Builder Component

8.15.0 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up a basic QueryBuilder component with predefined fields, an initial query, and state management using React hooks. It also shows the generated query structure.

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>
  );
};

view raw JSON →