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

Fix uncaught TypeError in Columns block #14605

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

- Add vertical alignment controls to `columns` Block ([#13899](https://github.com/WordPress/gutenberg/pull/13899/)).

### Bug Fixes

- fix uncaught error in `columns` block due to accessing a property on an object that might be undefined [#14605](https://github.com/WordPress/gutenberg/pull/14605)

## 2.3.0 (2019-03-06)

### New Feature
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/columns/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,18 @@ export const ColumnsEdit = function( { attributes, setAttributes, className, upd
);
};

const DEFAULT_EMPTY_ARRAY = [];

export default compose(
/**
* Selects the child column Blocks for this parent Column
*/
withSelect( ( select, { clientId } ) => {
const { getBlocksByClientId } = select( 'core/editor' );
const block = getBlocksByClientId( clientId )[ 0 ];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, I'm wondering if the following would work as well:

const { innerBlocks = DEFAULT_EMPTY_ARRAY } = getBlocksByClientId( clientId )[ 0 ];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That won't work if the array returned by getBlocksByClientId is empty. It'll result in something like:

"Can't destructure property 'innerBlocks' of 'undefined' or 'null'"


return {
childColumns: getBlocksByClientId( clientId )[ 0 ].innerBlocks,
childColumns: block ? block.innerBlocks : DEFAULT_EMPTY_ARRAY,
};
} ),
withDispatch( ( dispatch, { clientId, childColumns } ) => {
Expand Down