The existing answer is correct in pointing out that the CDN script has somewhat different workflow, but I was confused because import Form from "@rjsf/core";
didn't work for me on react-jsonschema-form
1.7.0:
Module not found: Error: Can't resolve '@rjsf/core'
It turns out there are two different packages. The older package (last updated in 2019 at the time of writing) react-jsonschema-form
worked for me with the following boilerplate:
import JSONSchemaForm from "react-jsonschema-form"; // 1.7.0
import React from "react"; // 17.0.2
const formSchema = {
type: "object",
properties: {
name: {
name: "Name",
type: "string",
pattern: "^[a-zA-Z]+$",
minLength: 10,
maxLength: 50,
},
},
required: ["name"],
};
const App = () => (
<JSONSchemaForm
onSubmit={f => console.log(f)}
schema={formSchema}
/>
);
export default App;
And here's a complete example of the newer package, @rjsf/core
4.2.2:
import Form from "@rjsf/core"; // 4.2.2
import React from "react"; // 17.0.2
const formSchema = {
type: "object",
properties: {
name: {
name: "Name",
type: "string",
pattern: "^[a-zA-Z]+$",
minLength: 10,
maxLength: 50,
},
},
required: ["name"],
};
const App = () => (
<Form
onSubmit={f => console.log(f)}
schema={formSchema}
/>
);
export default App;
For completeness, here's a CDN version as a stack snippet:
const Form = JSONSchemaForm.default;
// or const {default: Form} = JSONSchemaForm;
const schema = {
title: "Todo",
type: "object",
required: ["title"],
properties: {
title: {type: "string", title: "Title", default: "A new task"},
done: {type: "boolean", title: "Done?", default: false}
}
};
const log = (type) => console.log.bind(console, type);
ReactDOM.createRoot(document.querySelector("#app"))
.render(
<Form schema={schema}
onChange={log("changed")}
onSubmit={log("submitted")}
onError={log("errors")} />
);
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@rjsf/core@4.2.3/dist/react-jsonschema-form.js"></script>
<div id="app"></div>