createApi

[ ru ]

createApi is a shorthand for creating events attached to store by providing an object with reducers for them. If source store belongs to some domain then new events will also belong to it

Formulae

createApi(store, api): objectWithEvents

Arguments

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

Returns

(Object) Object with events

Example

import { createStore, createApi } from "effector";

const $playerPosition = createStore(0);

// create events and attach them to 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