Proposal: Using pure es6 base classes over Formsy.Mixins
See original GitHub issueI got Formsy working with pure es6 classes with some minor scaffolding. I wish to contribute it back so that Formsy can become pure es6 Object oriented without any Mixin.
I have been experimenting creating Formsy.Mixin a class and have been able to make it work with the following
import React from 'react';
/**
* The base component that autobinds all the methods to this.
*/
class BaseComponent extends React.Component {
constructor(props) {
super(props);
Object.getOwnPropertyNames(Object.getPrototypeOf(this)).forEach((method) => {
if (typeof this[method] !== 'function') {
return;
}
this[method] = this[method].bind(this);
});
}
}
export default BaseComponent;
import Formsy from 'formsy-react';
import BaseComponent from 'components/BaseComponent';
/**
* The base component that binds all Formsy Mixin methods so that they can be extended by es6 Formsy components.
*/
class FormComponent extends BaseComponent {
static defaultProps = {
validationError: '',
validationErrors: {}
}
constructor(props) {
super(props);
this.state = Object.assign(this.state || {}, Formsy.Mixin.getInitialState.bind(this)());
Object.getOwnPropertyNames(Formsy.Mixin).forEach((method) => {
if (typeof Formsy.Mixin[method] !== 'function') {
return;
}
if (method === 'getDefaultProps' || method === 'getInitialState') {
return;
}
this[method] = Formsy.Mixin[method].bind(this);
});
}
}
export default FormComponent;
Now a new form component is simply.
class MyFormComponent extends FormComponent {
}
Issue Analytics
- State:
- Created 8 years ago
- Reactions:4
- Comments:29 (1 by maintainers)
Top Results From Across the Web
Proposal: Using pure es6 base classes over Formsy.Mixins
I got Formsy working with pure es6 classes with some minor scaffolding. I wish to contribute it back so that Formsy can become...
Read more >Mixins Are Dead. Long Live Composition | by Dan Abramov
The introductory post made it clear that mixins are on their way out: Unfortunately, we will not launch any mixin support for ES6...
Read more >Mixins Considered Harmful – React Blog
We understand that more typing can be annoying. For the most common case, we plan to introduce a new base class called React.PureComponent...
Read more >What is the best way to use mixins in js? - Stack Overflow
The "mixins" are classes, created instantly by class-factories. They always extend another class. Thus it is pure inheritance. And even though ...
Read more >Please stop using classes in JavaScript - everyday.codes
Pre-ES6 classes. Even though the class keyword was added to JavaScript since ES6 (ECMAScript 2015), people were using classes earlier. The way ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In case anyone is interested in type declarations for TypeScript:
Example use:
FWIW, My team, is actively migrating away from Formsy to redux-form. We have tried to get the most out of Formsy. However redux has taken over our world, and redux-form solves some of the weird data cycles we see with Formsy and we find it easier to build form components with it.