Error while using react-jsonschema-form with material in typescript project

2.1k Views Asked by At

I am using react-jsonschema-form package with React, Material UI and Typescript. I am using the Material UI themed form called MuiForm5. I get compilation error for using this form and I am not able to resolve this. I am new to all these technologies. Any help is appreciated.

Giving here my code snipper, package dependencies and error.

I am following the documentation form this site https://react-jsonschema-form.readthedocs.io/en/latest/#installation and material UI specific documentation from here https://github.com/rjsf-team/react-jsonschema-form/tree/master/packages/material-ui

App.tsx:

import React from "react";
import "./App.css";
import { MuiForm5 as Form } from "@rjsf/material-ui";
import { JSONSchema7 } from "json-schema";

const schema: JSONSchema7 = {
  title: "Todo",
  type: "object",
  required: ["title"],
  properties: {
    title: { type: "string", title: "Title", default: "A new task" },
    done: { type: "boolean", title: "Done?", default: false },
  },
};

function App() {
  return (
    <div className='App'>
      <Form
        schema={schema}
        onChange={({ formData }) =>
          console.log("change", JSON.stringify(formData))
        }
        onSubmit={({ formData }) =>
          console.log("submit", JSON.stringify(formData))
        }
        onError={({ errors }) => console.log("error", JSON.stringify(errors))}
      />
    </div>
  );
}

export default App;

Dependencies in package.json:

{
  "dependencies": {
    "@emotion/react": "^11.9.0",
    "@emotion/styled": "^11.8.1",
    "@mui/icons-material": "^5.6.1",
    "@mui/material": "^5.6.1",
    "@rjsf/core": "^4.1.1",
    "@rjsf/material-ui": "^4.1.1",
    "@types/react": "^17.0.44",
    "@types/react-dom": "^17.0.15",
    "react": "^18.0.0",
    "react-dom": "^18.0.0",
    "react-scripts": "5.0.0",
    "typescript": "^4.6.3"
  },
  "devDependencies": {
    "@types/node": "^17.0.23",
    "@types/react-jsonschema-form": "^1.7.8"
  }
}

Compilation error:

ERROR in src/App.tsx:19:8
TS2786: 'Form' cannot be used as a JSX component.
  Its element type 'ReactElement<any, any> | Component<FormProps<any>, any, any> | null' is not a valid JSX element.
    Type 'Component<FormProps<any>, any, any>' is not assignable to type 'Element | ElementClass | null'.
      Type 'Component<FormProps<any>, any, any>' is not assignable to type 'ElementClass'.
        The types returned by 'render()' are incompatible between these types.
          Type 'React.ReactNode' is not assignable to type 'import("/Users/jayantwalvekar/Development/tests/my-jsonforms-app/node_modules/@types/react-transition-group/node_modules/@types/react/index").ReactNode'.
            Type '{}' is not assignable to type 'ReactNode'.
    17 |   return (
    18 |     <div className='App'>
  > 19 |       <Form
       |        ^^^^
    20 |         schema={schema}
    21 |         uiSchema={{}}
    22 |         onChange={({ formData }) =>
2

There are 2 best solutions below

1
On

//@ts-ignore ing the error works for me. i think that works because @rjsf/material-ui still uses class based component instead of functiona commponent

1
On

you need to install @rjsf/utils

package.json

{
....    
"dependencies": {
.....
"@rjsf/utils": "^5.0.0-beta.6",
...

}
}

and pass the validator to the form

import validator from '@rjsf/validator-ajv6';

.....

<Form
 ....
 validator={validator}
... 
/>