Skip to content

Commit

Permalink
Docs: Fix example in creating-dynamic-blocks.md (#10786)
Browse files Browse the repository at this point in the history
* Update creating-dynamic-blocks.md

While posts are loading inside the edit method the `posts` variable is null, attempting to access `post[0]` causes an error 

"TypeError: Cannot read property '0' of null" and the block displays "This block has encountered an error and cannot be previewed." 

checking that posts has a value resolves this.

* Added spacing to improve readability
  • Loading branch information
Daniel Gregory authored and ajitbohra committed Oct 24, 2018
1 parent a666a12 commit b022ad2
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions docs/blocks/creating-dynamic-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ registerBlockType( 'my-plugin/latest-post', {
posts: select( 'core' ).getEntityRecords( 'postType', 'post' )
};
} )( function( props ) {
if ( props.posts && props.posts.length === 0 ) {

if ( ! props.posts ) {
return "Loading...";
}

if ( props.posts.length === 0 ) {
return "No posts";
}
var className = props.className;
Expand Down Expand Up @@ -59,10 +64,16 @@ registerBlockType( 'my-plugin/latest-post', {
posts: select( 'core' ).getEntityRecords( 'postType', 'post' )
};
} )( ( { posts, className } ) => {

if ( ! posts ) {
return "Loading...";
}

if ( posts && posts.length === 0 ) {
return "No posts";
}
var post = posts[ 0 ];

let post = posts[ 0 ];

return <a className={ className } href={ post.link }>
{ post.title.rendered }
Expand Down

0 comments on commit b022ad2

Please sign in to comment.