Move default population to class function instead of using constructor (https://github.com/woocommerce/woocommerce-blocks/pull/1638)

This commit is contained in:
Mike Jolley 2020-01-23 14:11:15 +00:00 committed by GitHub
parent dd6afe0c70
commit 24d2600d43
1 changed files with 16 additions and 10 deletions

View File

@ -17,6 +17,20 @@ const withDefaultAttributes = createHigherOrderComponent(
constructor() {
super( ...arguments );
this.getAttributesWithDefaults = this.getAttributesWithDefaults.bind(
this
);
}
componentDidMount() {
const { block, setAttributes } = this.props;
if ( block.name.startsWith( 'woocommerce/' ) ) {
setAttributes( this.getAttributesWithDefaults() );
}
}
getAttributesWithDefaults() {
const blockType = getBlockType( this.props.block.name );
const attributes = Object.assign(
{},
@ -39,22 +53,14 @@ const withDefaultAttributes = createHigherOrderComponent(
} );
}
this.attributesWithDefaults = attributes;
}
componentDidMount() {
const { block, setAttributes } = this.props;
if ( block.name.startsWith( 'woocommerce/' ) ) {
setAttributes( this.attributesWithDefaults );
}
return attributes;
}
render() {
return (
<BlockListBlock
{ ...this.props }
attributes={ this.attributesWithDefaults }
attributes={ this.getAttributesWithDefaults() }
/>
);
}