Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React for Gutenberg, WordPress & Beyond - WordC...

Zac Gordon
October 06, 2018

React for Gutenberg, WordPress & Beyond - WordCamp Baltimore 2018

Zac Gordon

October 06, 2018
Tweet

More Decks by Zac Gordon

Other Decks in Technology

Transcript

  1. @zgordon // Normal React const el = React.createElement('h1', null, 'Welcome!');

    // WordPress React const el = wp.element.createElement('h1', null, 'Welcome!');
  2. @zgordon // Normal ReactDOM ReactDOM.render( el, document.getElementById( 'root' ) );

    // WordPress ReactDOM wp.element.render( el, document.getElementById( ‘root' ) );
  3. @zgordon // WordPress React - Normal const el = wp.element.createElement('h1',

    null, 'Welcome!'); // WordPress React - Destructured const { createElement } = wp.element; const el = createElement('h1', null, 'Welcome!');
  4. @zgordon <PanelBody> <TextControl label={ __( 'Text Field', 'domain' ) }

    value={ textControl } onChange={ textControl =>" setAttributes( { textControl } ) } /> </PanelBody> <PanelBody> <TextareaControl label={ __( 'Text Area Field', 'domain' ) } value={ textareaControl } onChange={ textareaControl =>" setAttributes( { textareaControl } ) } /> </PanelBody>
  5. @zgordon <PanelBody> <TextControl label={ __( 'Text Field', 'domain' ) }

    value={ textControl } onChange={ textControl =>" setAttributes( { textControl } ) } /> <TextareaControl label={ __( 'Text Area Field', 'domain' ) } value={ textareaControl } onChange={ textareaControl =>" setAttributes( { textareaControl } ) } /> </PanelBody>
  6. @zgordon const { Autocomplete, Button, CheckboxControl, ColorPalette, DropZone, FormToggle, Panel,

    Popover, Spinner, TextControl, TextareaControl, Tooltip, } = wp.components; const CustomTooltip = () =>" ( <Tooltip text="More Info"> <Button isDefault> Hover for Tooltip </Button> </Tooltip> );
  7. @zgordon const { withSelect } = wp.data; function MyAuthorsListBase( {

    authors } ) { return ( <ul> { authors.map( ( author ) =>" ( <li key={ author.id }>{ author.name }</li> ) ) } </ul> ); } const MyAuthorsList = withSelect( ( select ) =>" ( { authors: select( 'core' ).getAuthors(), } ) )( MyAuthorsListBase );
  8. @zgordon const { withSelect } = wp.data; function MyPostsListBase( {

    posts } ) { return ( <ul> { posts.map( ( post ) =>" ( <li key={ post.id }>{ post.title.rendered }</li> ) ) } </ul> ); } const MyPostList = withSelect( ( select ) =>" ( { posts: select( 'core' ).getEntityRecords( 'postType', 'post', { per_page: 5 } ) } ) )( MyPostsListBase );
  9. @zgordon edit: withSelect( select =>" { return { posts: select(

    'core' ).getEntityRecords( 'postType', 'post', { per_page: 5 } ) }; } )( ( { posts } ) =>" { return ( <ul> { posts.map( post =>" { return <li>{ post.title.rendered }</li>; }) } </ul> ); } )