Skip to main content
Version: Current (v7.0.0-beta.3)

Quickstart

Let's walk through setup of a form after installing the dependency properly.

note

The Form requires you to provide a validator implementation. We recommend the one from @rjsf/validator-ajv8.

Form schema​

First, specify a schema using the JSON Schema specification. The below schema renders a single string field:

import Form from '@rjsf/core';
import { RJSFSchema } from '@rjsf/utils';
import validator from '@rjsf/validator-ajv8';

const schema: RJSFSchema = {
title: 'Test form',
type: 'string',
};

export default function App() {
return <Form schema={schema} validator={validator} />;
}

To show App in a React app without a framework, such as one made with Vite, render it with createRoot(document.getElementById('root')!).render(<App />) from react-dom/client (leave out the ! in a JavaScript entry such as main.jsx).

You can also render an object with multiple fields with the below schema:

import Form from '@rjsf/core';
import { RJSFSchema } from '@rjsf/utils';
import validator from '@rjsf/validator-ajv8';

const schema: RJSFSchema = {
title: 'Test form',
type: 'object',
properties: {
name: {
type: 'string',
},
age: {
type: 'number',
},
},
};

export default function App() {
return <Form schema={schema} validator={validator} />;
}

For more information and examples of JSON Schema properties that this library supports, see Using JSON Schema.

Form uiSchema​

The uiSchema is used to add more customization to the form's look and feel. Use the classNames attribute of the uiSchema to add a custom CSS class name to the form:

import Form from '@rjsf/core';
import { RJSFSchema, UiSchema } from '@rjsf/utils';
import validator from '@rjsf/validator-ajv8';

const schema: RJSFSchema = {
title: 'Test form',
type: 'string',
};

const uiSchema: UiSchema = {
'ui:classNames': 'custom-css-class',
};

export default function App() {
return <Form schema={schema} uiSchema={uiSchema} validator={validator} />;
}

To customize object fields in the uiSchema, the structure of the uiSchema should be {key: value}, where key is the property key and value is an object with the uiSchema configuration for that particular property. For example:

import Form from '@rjsf/core';
import { RJSFSchema, UiSchema } from '@rjsf/utils';
import validator from '@rjsf/validator-ajv8';

const schema: RJSFSchema = {
title: 'Test form',
type: 'object',
properties: {
name: {
type: 'string',
},
age: {
type: 'number',
},
},
};

const uiSchema: UiSchema = {
name: {
'ui:classNames': 'custom-class-name',
},
age: {
'ui:classNames': 'custom-class-age',
},
};

export default function App() {
return <Form schema={schema} uiSchema={uiSchema} validator={validator} />;
}

Form initialization​

Often you'll want to prefill a form with existing data; this is done by passing an initialFormData prop object matching the schema:

import Form from '@rjsf/core';
import { RJSFSchema } from '@rjsf/utils';
import validator from '@rjsf/validator-ajv8';

const schema: RJSFSchema = {
type: 'object',
properties: {
title: {
type: 'string',
},
done: {
type: 'boolean',
},
},
};

const formData = {
title: 'First task',
done: true,
};

export default function App() {
return <Form schema={schema} initialFormData={formData} validator={validator} />;
}
note

If your form has a single field, pass a single value to initialFormData. ex: initialFormData="Charlie"

The form reads initialFormData when it mounts, so a parent re-rendering with it leaves the user's edits in place.

Form event handlers​

You can use event handlers such as onChange, onError, onSubmit, onFocus, and onBlur on the <Form /> component; see the Form Props Reference for more details.

Keeping a copy of the data​

<Form /> owns its data, as an uncontrolled input does, when you pass it initialFormData, which seeds it when the form mounts. Passed formData instead, it also shows each new value you pass after mount. To keep a copy of the value in your own state, store what onChange reports, as in the below example. To show another record, remount the form under a key.

import { useState } from 'react';
import Form from '@rjsf/core';
import validator from '@rjsf/validator-ajv8';

const App = () => {
const [formData, setFormData] = useState<string>();
return (
<Form
schema={{ type: 'string' }}
initialFormData={formData}
onChange={(e) => setFormData(e.formData)}
validator={validator}
/>
);
};

export default App;

Server components​

<Form /> is a client component, and every RJSF theme marks it as one, so a React Server Component, such as a page in the Next.js App Router, can import and render it directly:

import Form from '@rjsf/mui';
import validator from '@rjsf/validator-ajv8';

export default function Page() {
return <Form schema={{ type: 'string', title: 'Name' }} validator={validator} />;
}

A server component can pass only props that React can send to the client: the schemas, the data, the default validator and @rjsf/core/markdown's MarkdownTemplate, which reach Form as client references. Anything that is a function, or holds one, has to come from a module that starts with 'use client': event handlers such as onChange and onSubmit, customValidate, transformErrors, any widget, field or template other than MarkdownTemplate, a theme's own exports and getDefaultRegistry()'s included, and a validator made with customizeValidator() or createPrecompiledValidator(). The same goes for withTheme() and a theme's generateForm(), and for every export of @rjsf/chakra-ui and @rjsf/fluentui-rc, whose whole entry is a client module: call them in a client module.

Many samples in these docs call hooks such as useState or useRef, or pass functions such as onChange, widgets or templates (other than MarkdownTemplate), or any other prop that holds a function, such as a validator from customizeValidator(). To use one as a page in the Next.js App Router, put 'use client' at the top of its file.

In server code, such as a server component, a Server Action or a Route Handler, the default validator is only a reference, so its methods can't be called there; to validate data on the server, make a validator with customizeValidator().

A theme's own provider works as it does in any other app, though a provider that takes a value React can't send to the client, such as @chakra-ui/react's ChakraProvider with its system, has to be rendered from a client module of your own.