effector / patronum

☄️ Effector operators library delivering modularity and convenience ✨
https://patronum.effector.dev
MIT License
296 stars 43 forks source link

add `createChange` #122

Closed EvgenyiFedotov closed 4 years ago

EvgenyiFedotov commented 4 years ago

Example with source only

import { createStore } from "effector";
import { createChange } from "patronum/change";

const $name = createStore("");
const changeName = createChange({ source: $name });

$name.watch(console.log);
// => $name: ""

changeName("Bob");
// => $name: "Bob"

Example with prepend source

import { createStore } from "effector";
import { createChange } from "patronum/change";

const $name = createStore("");
const changeName = createChange({
  source: $name,
  prepend: (next, prev) => `${next}-${prev}`,
});

$name.watch(console.log);
// => $name: ""

changeName("Bob");
changeName("Alise");
changeName("John");
// => $name: "John-Bob-Alise"

Example with prepend source (for react)

import { createStore } from "effector";
import { createChange } from "patronum/change";

import React from "react";
import { useStore } from "effector-react";

const $name = createStore("");
const changeName = createChange({
  source: $name,
  prepend: (event) => event.currentTarget.value,
});

const Name = () => {
  const name = useStore($name);

  return <input value={name} onChange={changeName} />;
};

$name.watch(console.log);
// => $name: ""

// Typing in browser "Bob"
// => $name: "Bob"
sergeysova commented 4 years ago

It is looks like a createApi, isn't it?

EvgenyiFedotov commented 4 years ago

Yes, you right.

EvgenyiFedotov commented 4 years ago

So I close the current issue because it is kind of createApi.