import { createApi } from "effector";

createApi is a shortcut for generating events connected to a store by supplying an object with reducers for these events. If the source store is part of a domain, then the newly created events will also be within that domain.

Methods

createApi(store, api)

Formulae

createApi(store, api): objectWithEvents

Arguments

  1. store Store
  2. api (Object) An object with reducers

Returns

(Object) An object with events

Examples

import { createStore, createApi } from "effector";

const $playerPosition = createStore(0);

// Creating events and attaching them to the store
const api = createApi($playerPosition, {
  moveLeft: (pos, offset) => pos - offset,
  moveRight: (pos, offset) => pos + offset,
});

$playerPosition.watch((pos) => {
  console.log("position", pos);
});
// => position 0

api.moveRight(10);
// => position 10
api.moveLeft(5);
// => position 5

Try it

Contributors