I am in the middle of migrating a strapi v3 project to v4, but currently stuck at migrating a hook. I could not find any mention of v3-hooks in the migration guide and also the new Hook API does not help.
Anyone know how to accomplish this?
I’d like to have the same functionality in the v4 version, this is the current v3 state:
Hello all, sorry it took so long for us to see this one. You are correct in that hooks don’t have a direct replacement in Strapi v4 (largely because they weren’t used all often).
Generally speaking, hook packages should instead be converted into a plugin and the way you would interface with the hooks functions would be through strapi.plugin('plugin-name')...
So for example in the users-permissions plugin we can navigate down to the services here and call them from anywhere (I’m using the yarn strapi console here to explore the internal API
From the above we can see the various parts of the plugin, here what we care about is services, so we will list the services to see what exists:
And in my example I want to use the count function:
A similar example would be to build your plugin in such a way it registers getters, setters, functions, and other API methods on the global internal API such as in my own personal plugin for Redis support in Strapi:
In this plugin, I’m checking the plugin config and dynamically registering Redis clients per config option. I do this in the main bootstrap but it calls a local plugin service
Bootstrap:
The actual service that registers the Redis clients on the global API: