Move form's wrapped component instantiation to constructor

This commit is contained in:
SachaG 2017-11-09 10:01:51 +09:00
parent 9e959b96c6
commit 41b585adfa

View file

@ -35,6 +35,13 @@ import { withDocument } from 'meteor/vulcan:core';
class FormWrapper extends PureComponent {
constructor(props) {
super(props);
// instantiate the wrapped component in constructor, not in render
// see https://reactjs.org/docs/higher-order-components.html#dont-use-hocs-inside-the-render-method
this.FormComponent = this.getComponent();
}
// return the current schema based on either the schema or collection prop
getSchema() {
return this.props.schema ? this.props.schema : Utils.stripTelescopeNamespace(this.props.collection.simpleSchema()._schema);
@ -108,13 +115,7 @@ class FormWrapper extends PureComponent {
};
}
shouldComponentUpdate(nextProps) {
// prevent extra re-renderings for unknown reasons
// re-render only if the document selector changes
return nextProps.slug !== this.props.slug || nextProps.documentId !== this.props.documentId;
}
render() {
getComponent() {
// console.log(this)
@ -181,6 +182,16 @@ class FormWrapper extends PureComponent {
}
}
shouldComponentUpdate(nextProps) {
// prevent extra re-renderings for unknown reasons
// re-render only if the document selector changes
return nextProps.slug !== this.props.slug || nextProps.documentId !== this.props.documentId;
}
render() {
return this.FormComponent;
}
}
FormWrapper.propTypes = {