SlideShare a Scribd company logo
Manage Block Editor
State and Data
Ravi Chandra
Senior Web Engineer at 10up
New Gutenberg experience
wp.data
Built upon the same core principles of Redux
What is Redux ?
Redux is a popular JavaScript library for managing the state of your application
● Actions
○ An action is a plain JavaScript object that describes what happened. It has a type property which
describes the action and additional data/payload.
The three building blocks of Redux
● Reducers
○ A function that takes current state and action as arguments, and returns the next state of the app
● Store
○ Store is a Javascript object which holds the whole state of the app
wp.data
● Wrapper around redux
● Also available as a standalone npm package
@wordpress/data
● Methods
○ select
○ dispatch
○ Subscribe
● Has a registry which registers and maintains multiple
stores
wp.data.select
● Requires the store name argument and returns all the selectors
registered for that store.
○ E.g. wp.data.select(‘core/editor’)
● Default Gutenberg stores
○ core: WordPress Core Data
○ core/annotations: Annotations
○ core/blocks: Block Types Data
○ core/block-editor: The Block Editor’s Data
○ core/editor: The Post Editor’s Data
○ core/edit-post: The Editor’s UI Data
○ core/notices: Notices Data
○ core/nux: The NUX (New User Experience) Data
○ core/viewport: The Viewport Data
Code Examples
Get the current Post ID
Get the current Post object
Get the featured image ID
wp.data.dispatch
● Requires the store name argument and returns all the
actions registered for that store.
○ E.g. wp.data.dispatch(‘core/editor’)
● Triggers an action which in turn goes through a reducer
and updates the store.
Code Examples
Edit Post Title
Auto save Post
Save Post
wp.data.subscribe
● Subscribe to any change event to the state.
● Accepts a callback function which gets invoked when
any value in the store changes.
● This is global and not specific to any store.
Subscribing to change events
Putting it together
Display a notice to the user if the title has more than 10
characters
Give Me a Challenge
● Dynamically translate all blocks in a post.
● Translation API is expensive, so we want to limit the API calls.
● Add a “uuid” attribute to all the block types and set it as a
unique value for each block when the block is added to the
post.
● Based on the unique “uuid” value for each block we can
store the translated value for that block in post_meta using
the uuid as meta_key.
To
Custom Stores
wp.data.registerStore
● Add your own store to the centralized data registry
● Accepts two arguments
○ Name to identify the store.
○ Object with reducers, actions and selectors.
● After the store is registered, it can be accessed with
wp.data.select( ‘store-name’ ) or wp.data.dispatch( ‘store-name’
).
● Useful if you want to maintain a custom data store with your own
actions and reducers. E.g. Custom plugin for the block-editor.
Modify existing reducers
● wp.data registry is not exposed globally, which makes it
difficult to modify existing reducers.
● We can use `wp.data.use` to register a dummy plugin, the
`use` method returns the registry.
○ The `use` method might be deprecated in a future
release.
● Using the registry object we can access a specific store like
`core/data` and replace existing reducers or add our own.
Replace the default “Post updated.” notice when a post is
updated.
Resources
● https://gist.github.com/ravichdev/d467208b627e6de7b2aab185698c7
83f
● https://developer.wordpress.org/block-editor/packages/packages-data/
● https://medium.com/javascript-in-plain-english/the-only-introduction-to-
redux-and-react-redux-youll-ever-need-8ce5da9e53c6
● https://stackoverflow.com/questions/48104778/how-does-
reducereducers-work
● https://github.com/WordPress/gutenberg/tree/master/packages/data
Q&A
Thanks!

More Related Content

What's hot

Javascript
JavascriptJavascript
Javascript
Fahrizal Nuansa
 
Css selectors
Css selectorsCss selectors
Css selectors
Dinesh Kumar
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
Amit Tyagi
 
File systems virtualization in windows using mini filter drivers
File systems virtualization in windows using mini filter driversFile systems virtualization in windows using mini filter drivers
File systems virtualization in windows using mini filter drivers
MPNIKHIL
 
Dhtml ppt (2)
Dhtml ppt (2)Dhtml ppt (2)
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
Dhani Ahmad
 
from Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Worksfrom Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Works
National Cheng Kung University
 
React state
React  stateReact  state
React state
Ducat
 
jQuery
jQueryjQuery
Flask Introduction - Python Meetup
Flask Introduction - Python MeetupFlask Introduction - Python Meetup
Flask Introduction - Python Meetup
Areski Belaid
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
Digital Insights - Digital Marketing Agency
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
ShahDhruv21
 
Node js for beginners
Node js for beginnersNode js for beginners
Node js for beginners
Arjun Sreekumar
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHP
Shweta A
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
Malla Reddy University
 
Introduction to react js
Introduction to react jsIntroduction to react js
Introduction to react js
Aeshan Wijetunge
 
11 Java Script - Exemplos com eventos
11 Java Script - Exemplos com eventos11 Java Script - Exemplos com eventos
11 Java Script - Exemplos com eventos
Centro Paula Souza
 
Capitulo 14 -_componentes_gui_parte_3
Capitulo 14 -_componentes_gui_parte_3Capitulo 14 -_componentes_gui_parte_3
Capitulo 14 -_componentes_gui_parte_3
Pedro Neto
 
Dbms ii mca-ch10-concurrency-control-2013
Dbms ii mca-ch10-concurrency-control-2013Dbms ii mca-ch10-concurrency-control-2013
Dbms ii mca-ch10-concurrency-control-2013
Prosanta Ghosh
 
Jquery library
Jquery libraryJquery library

What's hot (20)

Javascript
JavascriptJavascript
Javascript
 
Css selectors
Css selectorsCss selectors
Css selectors
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
File systems virtualization in windows using mini filter drivers
File systems virtualization in windows using mini filter driversFile systems virtualization in windows using mini filter drivers
File systems virtualization in windows using mini filter drivers
 
Dhtml ppt (2)
Dhtml ppt (2)Dhtml ppt (2)
Dhtml ppt (2)
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 
from Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Worksfrom Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Works
 
React state
React  stateReact  state
React state
 
jQuery
jQueryjQuery
jQuery
 
Flask Introduction - Python Meetup
Flask Introduction - Python MeetupFlask Introduction - Python Meetup
Flask Introduction - Python Meetup
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
Node js for beginners
Node js for beginnersNode js for beginners
Node js for beginners
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHP
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Introduction to react js
Introduction to react jsIntroduction to react js
Introduction to react js
 
11 Java Script - Exemplos com eventos
11 Java Script - Exemplos com eventos11 Java Script - Exemplos com eventos
11 Java Script - Exemplos com eventos
 
Capitulo 14 -_componentes_gui_parte_3
Capitulo 14 -_componentes_gui_parte_3Capitulo 14 -_componentes_gui_parte_3
Capitulo 14 -_componentes_gui_parte_3
 
Dbms ii mca-ch10-concurrency-control-2013
Dbms ii mca-ch10-concurrency-control-2013Dbms ii mca-ch10-concurrency-control-2013
Dbms ii mca-ch10-concurrency-control-2013
 
Jquery library
Jquery libraryJquery library
Jquery library
 

Similar to Manage block editor state and data

Gutenberg Extended
Gutenberg ExtendedGutenberg Extended
Gutenberg Extended
Sören Wrede
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
Imran Sayed
 
Guvnor presentation jervis liu
Guvnor presentation jervis liuGuvnor presentation jervis liu
Guvnor presentation jervis liu
jbossug
 
Load testing in Zonky with Gatling
Load testing in Zonky with GatlingLoad testing in Zonky with Gatling
Load testing in Zonky with Gatling
Petr Vlček
 
Redux
ReduxRedux
JS Fest 2018. Anna Herlihy. How to Write a Compass Plugin
JS Fest 2018. Anna Herlihy. How to Write a Compass PluginJS Fest 2018. Anna Herlihy. How to Write a Compass Plugin
JS Fest 2018. Anna Herlihy. How to Write a Compass Plugin
JSFestUA
 
Dataweave Libraries and ObjectStore
Dataweave Libraries and ObjectStoreDataweave Libraries and ObjectStore
Dataweave Libraries and ObjectStore
Vikalp Bhalia
 
An approach to responsive, realtime with Backbone.js and WebSockets
An approach to responsive, realtime with Backbone.js and WebSocketsAn approach to responsive, realtime with Backbone.js and WebSockets
An approach to responsive, realtime with Backbone.js and WebSockets
Andrei Sebastian Cîmpean
 
Redux in Angular 2+
Redux in Angular 2+Redux in Angular 2+
Redux in Angular 2+
Hùng Nguyễn Huy
 
Scaling up and accelerating Drupal 8 with NoSQL
Scaling up and accelerating Drupal 8 with NoSQLScaling up and accelerating Drupal 8 with NoSQL
Scaling up and accelerating Drupal 8 with NoSQL
OSInet
 
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
Meetup  - Getting Started with MVVM Light for WPF - 11 may 2019Meetup  - Getting Started with MVVM Light for WPF - 11 may 2019
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
iFour Technolab Pvt. Ltd.
 
PyGrunn2013 High Performance Web Applications with TurboGears
PyGrunn2013  High Performance Web Applications with TurboGearsPyGrunn2013  High Performance Web Applications with TurboGears
PyGrunn2013 High Performance Web Applications with TurboGears
Alessandro Molina
 
Getting started with React and Redux
Getting started with React and ReduxGetting started with React and Redux
Getting started with React and Redux
Paddy Lock
 
Introduction to Container Storage Interface (CSI)
Introduction to Container Storage Interface (CSI)Introduction to Container Storage Interface (CSI)
Introduction to Container Storage Interface (CSI)
Idan Atias
 
Custom gutenberg block development with React
Custom gutenberg block development with ReactCustom gutenberg block development with React
Custom gutenberg block development with React
Imran Sayed
 
Updating materialized views and caches using kafka
Updating materialized views and caches using kafkaUpdating materialized views and caches using kafka
Updating materialized views and caches using kafka
Zach Cox
 
SH 1 - SES 6 - compass-tel-aviv-slides.pptx
SH 1 - SES 6 - compass-tel-aviv-slides.pptxSH 1 - SES 6 - compass-tel-aviv-slides.pptx
SH 1 - SES 6 - compass-tel-aviv-slides.pptx
MongoDB
 
Academy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingAcademy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & Tooling
Binary Studio
 
What’s new in MariaDB ColumnStore
What’s new in MariaDB ColumnStoreWhat’s new in MariaDB ColumnStore
What’s new in MariaDB ColumnStore
MariaDB plc
 
MySQL Time Machine by replicating into HBase - Slides from Percona Live Amste...
MySQL Time Machine by replicating into HBase - Slides from Percona Live Amste...MySQL Time Machine by replicating into HBase - Slides from Percona Live Amste...
MySQL Time Machine by replicating into HBase - Slides from Percona Live Amste...
Boško Devetak
 

Similar to Manage block editor state and data (20)

Gutenberg Extended
Gutenberg ExtendedGutenberg Extended
Gutenberg Extended
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
 
Guvnor presentation jervis liu
Guvnor presentation jervis liuGuvnor presentation jervis liu
Guvnor presentation jervis liu
 
Load testing in Zonky with Gatling
Load testing in Zonky with GatlingLoad testing in Zonky with Gatling
Load testing in Zonky with Gatling
 
Redux
ReduxRedux
Redux
 
JS Fest 2018. Anna Herlihy. How to Write a Compass Plugin
JS Fest 2018. Anna Herlihy. How to Write a Compass PluginJS Fest 2018. Anna Herlihy. How to Write a Compass Plugin
JS Fest 2018. Anna Herlihy. How to Write a Compass Plugin
 
Dataweave Libraries and ObjectStore
Dataweave Libraries and ObjectStoreDataweave Libraries and ObjectStore
Dataweave Libraries and ObjectStore
 
An approach to responsive, realtime with Backbone.js and WebSockets
An approach to responsive, realtime with Backbone.js and WebSocketsAn approach to responsive, realtime with Backbone.js and WebSockets
An approach to responsive, realtime with Backbone.js and WebSockets
 
Redux in Angular 2+
Redux in Angular 2+Redux in Angular 2+
Redux in Angular 2+
 
Scaling up and accelerating Drupal 8 with NoSQL
Scaling up and accelerating Drupal 8 with NoSQLScaling up and accelerating Drupal 8 with NoSQL
Scaling up and accelerating Drupal 8 with NoSQL
 
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
Meetup  - Getting Started with MVVM Light for WPF - 11 may 2019Meetup  - Getting Started with MVVM Light for WPF - 11 may 2019
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
 
PyGrunn2013 High Performance Web Applications with TurboGears
PyGrunn2013  High Performance Web Applications with TurboGearsPyGrunn2013  High Performance Web Applications with TurboGears
PyGrunn2013 High Performance Web Applications with TurboGears
 
Getting started with React and Redux
Getting started with React and ReduxGetting started with React and Redux
Getting started with React and Redux
 
Introduction to Container Storage Interface (CSI)
Introduction to Container Storage Interface (CSI)Introduction to Container Storage Interface (CSI)
Introduction to Container Storage Interface (CSI)
 
Custom gutenberg block development with React
Custom gutenberg block development with ReactCustom gutenberg block development with React
Custom gutenberg block development with React
 
Updating materialized views and caches using kafka
Updating materialized views and caches using kafkaUpdating materialized views and caches using kafka
Updating materialized views and caches using kafka
 
SH 1 - SES 6 - compass-tel-aviv-slides.pptx
SH 1 - SES 6 - compass-tel-aviv-slides.pptxSH 1 - SES 6 - compass-tel-aviv-slides.pptx
SH 1 - SES 6 - compass-tel-aviv-slides.pptx
 
Academy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingAcademy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & Tooling
 
What’s new in MariaDB ColumnStore
What’s new in MariaDB ColumnStoreWhat’s new in MariaDB ColumnStore
What’s new in MariaDB ColumnStore
 
MySQL Time Machine by replicating into HBase - Slides from Percona Live Amste...
MySQL Time Machine by replicating into HBase - Slides from Percona Live Amste...MySQL Time Machine by replicating into HBase - Slides from Percona Live Amste...
MySQL Time Machine by replicating into HBase - Slides from Percona Live Amste...
 

Recently uploaded

DELTA V MES EMERSON EDUARDO RODRIGUES ENGINEER
DELTA V MES EMERSON EDUARDO RODRIGUES ENGINEERDELTA V MES EMERSON EDUARDO RODRIGUES ENGINEER
DELTA V MES EMERSON EDUARDO RODRIGUES ENGINEER
EMERSON EDUARDO RODRIGUES
 
309475979-Creativity-Innovation-notes-IV-Sem-2016-pdf.pdf
309475979-Creativity-Innovation-notes-IV-Sem-2016-pdf.pdf309475979-Creativity-Innovation-notes-IV-Sem-2016-pdf.pdf
309475979-Creativity-Innovation-notes-IV-Sem-2016-pdf.pdf
Sou Tibon
 
Introduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.pptIntroduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.ppt
Dwarkadas J Sanghvi College of Engineering
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
uqyfuc
 
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
OKORIE1
 
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUESAN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
drshikhapandey2022
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
UNIT-III- DATA CONVERTERS ANALOG TO DIGITAL CONVERTER
UNIT-III- DATA CONVERTERS ANALOG TO DIGITAL CONVERTERUNIT-III- DATA CONVERTERS ANALOG TO DIGITAL CONVERTER
UNIT-III- DATA CONVERTERS ANALOG TO DIGITAL CONVERTER
vmspraneeth
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
upoux
 
openshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoinopenshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoin
snaprevwdev
 
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Transcat
 
Open Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surfaceOpen Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surface
Indrajeet sahu
 
This study Examines the Effectiveness of Talent Procurement through the Imple...
This study Examines the Effectiveness of Talent Procurement through the Imple...This study Examines the Effectiveness of Talent Procurement through the Imple...
This study Examines the Effectiveness of Talent Procurement through the Imple...
DharmaBanothu
 
Beckhoff Programmable Logic Control Overview Presentation
Beckhoff Programmable Logic Control Overview PresentationBeckhoff Programmable Logic Control Overview Presentation
Beckhoff Programmable Logic Control Overview Presentation
VanTuDuong1
 
Blood finder application project report (1).pdf
Blood finder application project report (1).pdfBlood finder application project report (1).pdf
Blood finder application project report (1).pdf
Kamal Acharya
 
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
sydezfe
 
Unit -II Spectroscopy - EC I B.Tech.pdf
Unit -II Spectroscopy - EC  I B.Tech.pdfUnit -II Spectroscopy - EC  I B.Tech.pdf
Unit -II Spectroscopy - EC I B.Tech.pdf
TeluguBadi
 
SELENIUM CONF -PALLAVI SHARMA - 2024.pdf
SELENIUM CONF -PALLAVI SHARMA - 2024.pdfSELENIUM CONF -PALLAVI SHARMA - 2024.pdf
SELENIUM CONF -PALLAVI SHARMA - 2024.pdf
Pallavi Sharma
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
upoux
 

Recently uploaded (20)

DELTA V MES EMERSON EDUARDO RODRIGUES ENGINEER
DELTA V MES EMERSON EDUARDO RODRIGUES ENGINEERDELTA V MES EMERSON EDUARDO RODRIGUES ENGINEER
DELTA V MES EMERSON EDUARDO RODRIGUES ENGINEER
 
309475979-Creativity-Innovation-notes-IV-Sem-2016-pdf.pdf
309475979-Creativity-Innovation-notes-IV-Sem-2016-pdf.pdf309475979-Creativity-Innovation-notes-IV-Sem-2016-pdf.pdf
309475979-Creativity-Innovation-notes-IV-Sem-2016-pdf.pdf
 
Introduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.pptIntroduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.ppt
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
 
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUESAN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
UNIT-III- DATA CONVERTERS ANALOG TO DIGITAL CONVERTER
UNIT-III- DATA CONVERTERS ANALOG TO DIGITAL CONVERTERUNIT-III- DATA CONVERTERS ANALOG TO DIGITAL CONVERTER
UNIT-III- DATA CONVERTERS ANALOG TO DIGITAL CONVERTER
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
 
openshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoinopenshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoin
 
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
 
Open Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surfaceOpen Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surface
 
This study Examines the Effectiveness of Talent Procurement through the Imple...
This study Examines the Effectiveness of Talent Procurement through the Imple...This study Examines the Effectiveness of Talent Procurement through the Imple...
This study Examines the Effectiveness of Talent Procurement through the Imple...
 
Beckhoff Programmable Logic Control Overview Presentation
Beckhoff Programmable Logic Control Overview PresentationBeckhoff Programmable Logic Control Overview Presentation
Beckhoff Programmable Logic Control Overview Presentation
 
Blood finder application project report (1).pdf
Blood finder application project report (1).pdfBlood finder application project report (1).pdf
Blood finder application project report (1).pdf
 
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
 
Unit -II Spectroscopy - EC I B.Tech.pdf
Unit -II Spectroscopy - EC  I B.Tech.pdfUnit -II Spectroscopy - EC  I B.Tech.pdf
Unit -II Spectroscopy - EC I B.Tech.pdf
 
SELENIUM CONF -PALLAVI SHARMA - 2024.pdf
SELENIUM CONF -PALLAVI SHARMA - 2024.pdfSELENIUM CONF -PALLAVI SHARMA - 2024.pdf
SELENIUM CONF -PALLAVI SHARMA - 2024.pdf
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
 

Manage block editor state and data

  • 1. Manage Block Editor State and Data Ravi Chandra Senior Web Engineer at 10up
  • 3. wp.data Built upon the same core principles of Redux
  • 4. What is Redux ? Redux is a popular JavaScript library for managing the state of your application
  • 5. ● Actions ○ An action is a plain JavaScript object that describes what happened. It has a type property which describes the action and additional data/payload. The three building blocks of Redux ● Reducers ○ A function that takes current state and action as arguments, and returns the next state of the app ● Store ○ Store is a Javascript object which holds the whole state of the app
  • 6.
  • 7. wp.data ● Wrapper around redux ● Also available as a standalone npm package @wordpress/data ● Methods ○ select ○ dispatch ○ Subscribe ● Has a registry which registers and maintains multiple stores
  • 8. wp.data.select ● Requires the store name argument and returns all the selectors registered for that store. ○ E.g. wp.data.select(‘core/editor’) ● Default Gutenberg stores ○ core: WordPress Core Data ○ core/annotations: Annotations ○ core/blocks: Block Types Data ○ core/block-editor: The Block Editor’s Data ○ core/editor: The Post Editor’s Data ○ core/edit-post: The Editor’s UI Data ○ core/notices: Notices Data ○ core/nux: The NUX (New User Experience) Data ○ core/viewport: The Viewport Data
  • 10. Get the current Post ID
  • 11. Get the current Post object
  • 12. Get the featured image ID
  • 13. wp.data.dispatch ● Requires the store name argument and returns all the actions registered for that store. ○ E.g. wp.data.dispatch(‘core/editor’) ● Triggers an action which in turn goes through a reducer and updates the store.
  • 18. wp.data.subscribe ● Subscribe to any change event to the state. ● Accepts a callback function which gets invoked when any value in the store changes. ● This is global and not specific to any store.
  • 21. Display a notice to the user if the title has more than 10 characters
  • 22.
  • 23.
  • 24. Give Me a Challenge
  • 25. ● Dynamically translate all blocks in a post. ● Translation API is expensive, so we want to limit the API calls. ● Add a “uuid” attribute to all the block types and set it as a unique value for each block when the block is added to the post. ● Based on the unique “uuid” value for each block we can store the translated value for that block in post_meta using the uuid as meta_key.
  • 26.
  • 27.
  • 28.
  • 29. To
  • 31. wp.data.registerStore ● Add your own store to the centralized data registry ● Accepts two arguments ○ Name to identify the store. ○ Object with reducers, actions and selectors. ● After the store is registered, it can be accessed with wp.data.select( ‘store-name’ ) or wp.data.dispatch( ‘store-name’ ). ● Useful if you want to maintain a custom data store with your own actions and reducers. E.g. Custom plugin for the block-editor.
  • 32.
  • 33.
  • 34.
  • 35.
  • 37. ● wp.data registry is not exposed globally, which makes it difficult to modify existing reducers. ● We can use `wp.data.use` to register a dummy plugin, the `use` method returns the registry. ○ The `use` method might be deprecated in a future release. ● Using the registry object we can access a specific store like `core/data` and replace existing reducers or add our own.
  • 38. Replace the default “Post updated.” notice when a post is updated.
  • 39.
  • 40.
  • 41.
  • 42. Resources ● https://gist.github.com/ravichdev/d467208b627e6de7b2aab185698c7 83f ● https://developer.wordpress.org/block-editor/packages/packages-data/ ● https://medium.com/javascript-in-plain-english/the-only-introduction-to- redux-and-react-redux-youll-ever-need-8ce5da9e53c6 ● https://stackoverflow.com/questions/48104778/how-does- reducereducers-work ● https://github.com/WordPress/gutenberg/tree/master/packages/data
  • 43. Q&A

Editor's Notes

  1. I am Ravi, I am a Senior Web Engineer at 10up. I am going to share with you today the Gutenberg's data module and how to manage its state and data. Lets take a quick poll, Show of hands: How many of you are developers here ? How many of you here have developed a custom Gutenberg block ? How many of you have looked at the wp.data API and it’s various methods, I mean like really looked at it ? This talk is developer focused, and I would be showing some code examples. I hope that at least some of you who are interested in Gutenberg development will get some useful information. If you are already a pro at Gutenberg, I have couple of challenging use cases with solutions at the end that might interest you.
  2. If you are used to the classic WordPress editor and metaboxes, customizing Gutenberg looks intimidating at first. It might look like it's not very developer friendly. It can also be frustrating or at least challenging at times, having to learn a completely new thing when you are so used to the classic editor.
  3. Customizing Gutenberg is really easy, it has an awesome data module under the hood which lets you manage and share data, subscribe and trigger events. It's built upon the same core principles of Redux. OOO
  4. Redux 101: Let’s briefly talk about what redux is. OOO A state in Redux is a JavaScript object, where the internal state of the application is stored as its properties, e.g. which user is logged on, the title or content of a post or name and email of a user.
  5. OOO Action: A user logged in, post title is updated etc. Action has mainly 2 properties, the type which describes the action type and payload or data which contains data required by the action. OOO Reducers: Lets say after a user logs in we update `isLoggedIn` property in our state. Reducer is where we would do that. Reducer functions check for the action type and use the action data to update the store. OOO Store: Store is what holds the whole state of the app
  6. This is Redux in a nutshell We have the UI which subscribes to the store and listens for events whenever the state changes. From the UI we dispatch an action whenever we want to update the state of our application. The reducer checks for action types and performs the state updation. The store is a Javascript Object deep within the browser’s window object with restricted scope. We would not have direct access to it and need to use special methods called as selectors to read any specific data in the store.
  7. wp.data is a Javascript object available in the browser if Gutenberg editor is loaded. Using the wp.data module we could listen to any changes in the editor like title, post content, and perform a custom action like checking the content length and so on. OOO It’s a wrapper around redux and exposes some of the redux methods along with its own methods. OOO OOO Methods: Select to read data from the store Dispatch to dispatch a custom action Subscribe to subscribe to the store changes
  8. wp.data.select provides an easy way to access a store selectors and thereby read data/state from the store. OOO It requires the store key argument and returns an object with all the selectors registered for that store. OOO Gutenberg has default stores used by the editor. We would be focusing more on the core/editor store today in all our examples.
  9. Lets look at some code examples we all have been waiting for
  10. Returns the current post id.
  11. Returns the current post as a JavaScript object, also includes any registered custom meta.
  12. Returns the edited post attribute, defaults to the saved value if the attribute is not edited. getCurrentPostAttribute returns the current/saved attribute. There are so many selector methods available in all the data stores. Use Google Chrome console to log the selector object to see all the available methods.
  13. OOO OOO Use wp.data.dispatch to trigger actions. Gutenberg has a lot of actions included.
  14. Update the post title in the editor. editPost accepts an object with the values you want to update
  15. Trigger the post auto-save.
  16. Trigger the post save.
  17. OOO OOO OOO This callback will be triggered whenever state is changed in any of the registered stores.
  18. Lets see an example of subscribe, select and dispatch together
  19. Lets say this is what we want to build.
  20. Get the post current saved title In the subscribe callback, get the edited/updated title in the post editor Check if the title has since been changed, if it’s changed, set the default title to newTitle. If the title is more than 10 characters dispatch a notice to the editor If it’s less than 10 characters, we simply remove an existing notice. The default notice reducer checks if a notice with the provided id exists and if it does it removes it else it just ignores the action. Lets see what we built in action.
  21. So we looked at a basic example of using the wp.data module, let’s take a look at a challenging one.
  22. END To achieve this we need a way to assign a unique key to each block. The key should not change on subsequent post/block updates.
  23. We are using WP JS hooks `addFilter` method on the `blocks.registerBlockType` to filter the block attributes being registered. This filter would run for any block registered with the `registerBlockType` method. We are extending the attributes property by adding the uuid attribute with type string
  24. We have a wrapper method onPostUpdateSuccessListener which takes a listener argument (which is a method) and returns a function. We check if the post is not saving or is not auto saving and check if the save succeeded. If it passes all these conditions, it means that the post update was successful and we invoke the listener callback
  25. Here we are innvoking the subscrie method with our custom wrapper function and passing a function as it’s callback As we have seen in the last slide the callback will be invoked when the post save is succesful In our callback we get all the blocks inn the post, iterate through them and check if the block has a uuid attribute set. If it’s not set we keep track of the block clientID Then for each of the blocks with no uuid attribute we trigger the updateBlockAttributes method and set the uuid.
  26. That results in this paragraph block converted to this.
  27. So far we have looked at using WP default stores, let’s take a look at how we can register and use our own custom stores
  28. We aer setting default state. We have 2 custom actions here, addItem and removeItem. Each action returns an object with the type property which describes the action and additional data/payload. The action type is what we use in the reducer to check for specific action and perform our reducer function.
  29. We have a selector getItems here which takes the state, reads the items from the state and returns it.
  30. Finally we register our store using the wp.data.registerStore function, pass the store name and the oject with our actions, reducers and selectors In our reducer we have 2 case statements one to handle ADD and other to handle remove. The add reducer just adds the item to our state and thge remove filters out the item and thery removes it from the state.
  31. Code examples of how to add, remove and get items
  32. So we have all these reducers already defined in the wp.data module like the notices reducer or the core/editor reducers. Is there a way we can add our own reducer function ? Maybe we want to add an additional property to the state when a post is saved. Lets see how we can do that
  33. Register a dummy plugin with empty object and get access to the registry Get the defaultNoticeReducer Write our own custom reducer function which checks for the CREATE NONTICE action and also checks if the notice is is SAVE_POST_NOTICE_ID. This notice id is only for the Post Updated notice, we know that because we looked at the wp.data notice store action source code. Then we find the index of our notice and replace the content value of the notice and we make additional changes to return the expected state.
  34. ReduceReducers is a custom function which takes a list of reducers and combines them all into one so that each reducer is called sequentially for an action. We call the replaceReducer method which comes from Redux and pass our new reducer object which contains all the reducers.
  35. And this is the result. The post updated message is updated. Using this method we can customize a lot of default functionality within Gutenberg. Spend some time looking through all the actions and reducers in wp.data package.
  36. Some additional resources