Migrating v2 to v3
This guide is designed to help you through the migration. If you went through it and encountered any problems - do let us know. For more information on why certain changes were made, see the CHANGELOG.md. When migrating to v3, use the newest version. Gradual updates will take more time and won't ease this process.
Breaking API changes
- Context data shape has changed:
changed,changedMap,submitting, andvalidatingwere lifted from thestateproperty to the root. - Removed
AutoForm.state.modelSync. UseAutoForm.state.modelinstead. - Removed
BaseField. UseconnectFieldoruseFieldinstead. - Removed
BaseForm.getChangedKeys. UsechangedKeysdirectly. - Removed
BaseForm.state.bridge. UseBaseForm.props.schemainstead. - Removed
Bridge.check. WithoutcreateSchemaBridgeit's no longer needed. - Removed
baseFieldfromconnectFieldoptions. There's no one solution here and it may require additional changes, depending on the usage. - Removed
createSchemaBridge. Now all*Bridgeinstances have to be created manually.import { SimpleSchema } from 'simpl-schema';
+ import { SimpleSchema2Bridge } from 'uniforms-bridge-simple-schema-2';
const schema = new SimpleSchema({ /* ... */ });
- <AutoForm schema={schema} />
+ const bridge = new SimpleSchema2Bridge(schema);
+ <AutoForm schema={bridge} /> - Removed
ensureValuefromconnectFieldoptions. That meansundefinedwill no longer be automatically passed to the field as''. Usevalue ?? ''instead. This option was enabled by default, therefore it will impact all your custom fields. - Removed
includeParentfromconnectFieldoptions. UseuseFieldas many times as needed instead.const parentName = joinName(joinName(null, props.name).slice(0, -1));
const parentField = useField(parentName, {}, { absoluteName: true })[0]; - Removed
injectName. In most cases, it can be safely omitted. - Removed
mapPropsfromconnectFieldoptions. Map props directly in the component. - Removed
nothing. Usenullinstead. - Removed all
propTypesin favor of TypeScript types. - Renamed or removed deprecated lifecycle methods. If you were using them, e.g.
super.componentWillReceiveProps, check whether it's still there and use the correct name if needed. - Renamed
getChildContext*methods togetContext*, e.g.getChildContextName->getContextName. - Synchronous return and throw in
onSubmitare no longer allowed. To return an error or some result, return aPromiseinstead. filterDOMProps.registeredis now read-only.
Validation flow changes
- Bridge validators have to return errors instead of throwing them.
// GraphQL Schema
function validator(model) {
if (errors.length) {
- throw { details: validator.errors };
+ return { details: validator.errors };
}
}// JSON Schema
function createValidator(schema) {
const validator = ajv.compile(schema);
return (model) => {
validator(model);
if (validator.errors && validator.errors.length) {
- throw { details: validator.errors };
+ return { details: validator.errors };
}
};
} - Removed
onSubmitSuccessandonSubmitFailure. Perform all needed operations directly in theonSubmit:- onSubmit={onSubmit}
- onSubmitSuccess={onSubmitSuccess}
- onSubmitFailure={onSubmitFailure}
+ onSubmit={model => {
+ const result = onSubmit(model);
+ result.then(onSubmitSuccess, onSubmitFailure);
+ return result;
+ }}` onValidateis no longer using callbacks. The error (or the lack of it) has to be returned either synchronously or asynchronously (i.e. wrapped in a promise).- onValidate={(model, error, done) => done(error)}
+ onValidate={async (model, error) => error}
React Context API
- If you were not using
context,contextTypes,childContextTypes, orgetChildContext*methods directly, there's nothing to do. - For direct context access, use
useFormhook (functional components),contextTypestatic property (class components), or<context.Consumer />(both).- The React context object,
context, is exported from theuniformspackage.
- The React context object,
TypeScript
- A lot of types were added or changed. If you are using TypeScript, you may expect some type errors, as all components are no longer full of
any. filterDOMProps.registeris now type safe and requiresFilterDOMPropsinterface extension.
Miscellaneous
- For performance reasons
getField,getSubfields, andgetTypeof all bridges are now memoized. If possible, do the same for custom bridges for a potential performance gain. - Simplified
NumFieldin most themes as it works as expected in React 16 and later. If you have a customNumFieldin your project, do revise its implementation for a potential performance gain. - Stop using direct imports and use named ones instead. It'll let your bundler decide, which version it'll need.
-import BaseForm from 'uniforms/BaseForm';
+import { BaseForm } from 'uniforms';