Wednesday, 21 November 2018

How to use the @wordpress/hooks package in JavaScript Apps


WordPress Plugin API (hooks) are a great way to make your plugins or themes extensible. And that is something that you can use when using PHP. But what if you’re using JavaScript? Maybe you want to make your JavaScript project extensible in a similar way. In this tutorial, we will see how to use those hooks in a React App. What is @wordpress/hooks?
Before we write any code, let’s understand how we can use those JavaScript hooks. So, @wordpress/hooks is a package, an npm package which you can check out on npmjs.
If your JavaScript app is running under WordPress and Gutenberg is active, you can use it without installing anything by calling it from wp.hooks.
Otherwise, you can include that in your JavaScript app by installing it like this:
npm install @wordpress/hooks –save
Creating your Hook environment
To create your hook environment, you can create a global object that will be accessible throughout your entire app. Or you can use an internal object which you’ll pass to components using the hook system.
import { createHooks } from ‘@wordpress/hooks’;
Now you can use the createHooks function to start your Hook environment like this:
var myGlobalHooks
Source: https://managewp.org/articles/18131/how-to-use-the-wordpress-hooks-package-in-javascript-apps



source https://williechiu40.wordpress.com/2018/11/21/how-to-use-the-wordpress-hooks-package-in-javascript-apps/

No comments:

Post a Comment