Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form with constraints components dont work with react 18 #58

Closed
enqbator opened this issue Apr 7, 2022 · 5 comments
Closed

Form with constraints components dont work with react 18 #58

enqbator opened this issue Apr 7, 2022 · 5 comments

Comments

@enqbator
Copy link

enqbator commented Apr 7, 2022

When building with webpack / typescript / @babel/react react-form-with-constrains components prevent my project from building after updating to react 18.

Build functions again after downgrading back to react 17.

"FieldFeedbacks cannot be used as a jsx component"

"Property 'children' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & Pick<Readonly, never> & Partial<...>'."

@enqbator
Copy link
Author

To clarify, it looks to be caused breaking changes in the react typescript bindings.

DefinitelyTyped/DefinitelyTyped#56210

@andrec93p
Copy link

andrec93p commented Jun 10, 2022

After struggling with a project just upgraded to React 18, I actually found a workaround. As far as I'm aware, the issue is just related to the typing, so it can be patched with some TypeScript, but it doesn't look pretty. You can do it like this.

  1. Create inside your src folder the file react-form-with-constraints-bootstrap.d.ts (if you're using Bootstrap of course). This will override the type definition for the module.
  2. Use this as the content.
import {ReactNode} from "react";
import {
  FieldFeedbacksProps as FieldFeedbacksPropsBase,
} from "react-form-with-constraints";
import {FieldFeedbacksContext} from "react-form-with-constraints/lib/FieldFeedbacks";

// Use the path here, NOT the just the name.
import {
  FieldFeedbacks as FieldFeedbacksBase,
} from "../node_modules/react-form-with-constraints-bootstrap";

export interface FieldFeedbacksProps extends FieldFeedbacksPropsBase {
  children?: ReactNode; // <- This adds the capability to use children...
}

export declare class FieldFeedbacks extends FieldFeedbacksBase {
  static defaultProps: FieldFeedbacksProps;
  constructor(props: FieldFeedbacksProps, context: FieldFeedbacksContext);
  render(): null; // <- ...and this prevents the "Cannot be used as a JSX component" error.
}

export {
  FieldFeedback,
  FieldFeedbackTooltip,
  Input,
  Async,
  FormWithConstraints,
} from "../node_modules/react-form-with-constraints-bootstrap";

Basically this overrides the type declarations for the module making them compatible with React 18. The ugly part is that in order to make the render method work you have to redefine it as a method returning just NULL, or the declaration won't be compatible with the base class one.

The actual fix should be quite trivial though, I'll try opening a PR if I have a moment.

@enqbator
Copy link
Author

@andrec93p Thank you for that. I was looking at having to use something else for validation, which is a shame. Every other validation package seems to be way more complicated than they need to be and just plain invasive in the way they affect how you have to make the form/access the values.

It would have meant reworking all the markup and even loading / saving logic in all of my apps forms.

Thanks again.

@tkrotoff
Copy link
Owner

tkrotoff commented Jul 9, 2022

Big PR that fixes this (among other things): #59

@tkrotoff
Copy link
Owner

Fix published in v0.19.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants