AppmakerDeveloper

Register a custom action

This doc will help you to register a custom action in your appmaker extension and use it in your app.

Description

This doc will help you to register a custom action in your appmaker extension and use it in your app.

Steps

  1. Create an actions folder and add index.js inside actions in your project root directory. Eg: src/actions/index.js
  2. Add the following code to the file.

import { appmaker } from '@appmaker-xyz/core';

function MY_CUSTOM_ACTION(
  { action, params },
  { handleAction, appStorageState },
) {
  console.log("params", params);
  
  // you can write the logic for your action here
  alert('My custom action is called');
}

function MY_SECOND_ACTION(
  { action, params },
  { handleAction, appStorageState },
) {
  // You can also call another action from using handleAction
  handleAction({
    action: 'MY_CUSTOM_ACTION',
    params: {
      key: 'value',
    },
  });
}

export function registerActions() {
  appmaker.actions.registerAction('MY_CUSTOM_ACTION', MY_CUSTOM_ACTION);
  appmaker.actions.registerAction('MY_SECOND_ACTION', MY_SECOND_ACTION);
}
  1. Import the registerActions function in your root file src/index.js and call it in activate function.

import { registerActions } from './actions';

export function activate() {
  registerActions();
}

Usage

You can now use the action in your app using onAction.

onAction({
  "action": "MY_CUSTOM_ACTION",
  "params": {
    "key": "value"
  }
});