# Hooks Explained *The behaviour of hooks is heavily inspired by [WordPress Hooks](https://developer.wordpress.org/plugins/hooks/).* It is highly recommended to get familiar with how **WordPress hooks** work first There are three types of hooks - actions - filters - react_hooks (frontend only) The key differences from WordPress are: 1. Actions/Filters are declared statically not dynamically 1. Hooks are always asynchronous in the backend 1. Backend hooks require the "Request" object of incoming request as the first parameter 1. For actions the return_value is undefined 1. For filters the return_value is initialized as the first argument 1. For both actions and filters callbacks the first parameter is a `context` object. In the frontend is empty, in the backend it has the `req` property which contains the request object 1. For both actions and filters callbacks the second parameter is the `return_value` received from the last module You can use hooks and filters from anywhere in the code as the functions are globally provided: - use_frontend_action , use_frontend_action , use_action - use_frontend_filter , use_backend_filter, use_filter --- --- ## Action Hooks Actions allow you to execute functionality and return a value from them Frontend: ```js frontendConnector({ hooks: { actions: [{ // return_value is the result returned by the previous executed hook // arg1, arg2, ...args are the argument passed when triggering the hook hook_action_name: (context, return_value, arg1, arg2, ...args) => { return return_value; } }] } }); // use_frontend_action is globally available, no need to import it const value_from_action = use_frontend_action.hook_action_name(1, 2, 3); console.log('value: ' + value_from_action); ``` Backend: ```js import express from 'express'; import { onRequestHook_Express } from '@stlse/backend-connector'; const app = express(); backendConnector({ onRequest: onRequestHook_Express(app), hooks: { actions: [{ // return_value is the result returned by the previous executed hook // arg1, arg2, ...args are the argument passed when triggering the hook different_name_actions: (context, return_value, arg1, arg2, ...args) => { console.log(context.req) // has the req object return return_value; } }] } }) app.get('/', async (req, res, next) => { try { // all hooks are async in backend, so we must await // use_frontend_action is globally available, no need to import it const value_from_action = await use_frontend_action.different_name_actions(req, 1, 2, 3); res.send({value: value_from_action}) } catch (e) { next(e); } }) ``` --- --- ## Filter Hooks Filters allow you to expose a value to modify and then return a different value Frontend: ```js frontendConnector({ hooks: { filters: [{ // return_value is the result returned by the previous executed hook // arg1, arg2, ...args are the argument passed when triggering the hook hook_filter_name: (context, return_value, arg1, arg2, ...args) => { return return_value; } }] } }); // use_frontend_filter is globally available, no need to import it const filtered_value = use_frontend_filter.hook_filter_name(1, 2, 3); console.log('value: ' + filtered_value); ``` Backend: ```js import express from 'express'; import { onRequestHook_Express } from '@stlse/backend-connector'; const app = express(); backendConnector({ onRequest: onRequestHook_Express(app), hooks: { filters: [{ // return_value is the result returned by the previous executed hook // arg1, arg2, ...args are the argument passed when triggering the hook different_name_filters: (context, return_value, arg1, arg2, ...args) => { console.log(context.req) // has the req object return return_value; } }] } }) app.get('/', async (req, res, next) => { try { // all hooks are async in backend, so we must await // use_backend_filter is globally available, no need to import it const filtered_value = await use_backend_filter.different_name_filters(req, 1, 2, 3); res.send({value: filtered_value}) } catch (e) { next(e); } }) ``` --- --- ## React Hooks React Hooks are similar to filters, you can specify where a hook should be and other modules can inject their own component into the hook. They work as normal react component and you can pass props. They even work with different React versions! Frontend only: ```js frontendConnector({ hooks: { react: [{ // otherComponents is an Array of all the components returned by previous hooks name_of_hook: (context, otherComponents) => MyComponent, }] } }); function MyComponent(props) { return (