Code Monkey home page Code Monkey logo

effector-formula's Introduction

Effector formula

Simple and minimal "formula" creator for effector when working with React, built with TypeScript, for reusable and scoped stores.

Inspired by Context+hooks state management, but with effector.

Example

Create a function (e.g. CounterStore) that accepts StoreProps and returns effector stores and events and pass it to createStoreFormula. StoreProps include lifecycle "hooks" (onMount, onUnmount) and other desired stores.

You get back:

  • React Provider: When placed in the desired component hierarchy, the function (e.g. CounterStore) gets executed and the effector stores are created. When the component gets out of scope, everything is cleaned up.

  • Selector hooks for all the effector stores

  • Events

  • The functions' returned value itself

  • Context to be used for getting current stores' reference in another store

"Store" definition:

///// CounterStore.tsx

function CounterStore({ onMount, onUnmount }: StoreProps<[]>) {
  const count = createDomain("count")
  const increment = count.createEvent()
  const decrement = count.createEvent()
  const reset = count.createEvent()

  const $count = count.createStore(0)
          .on(increment, c => c + 1)
          .on(decrement, c => c - 1)
          .reset(reset)

  // ... other effector stores & events

  onMount(() => {
    console.log("Hi :)")
  })

  onUnmount(() => {
    console.log("Bye :)")
  })

  return {
    $count,
    increment,
    decrement,
    reset
  }
}


export const [
  CounterStoreProvider,
  useCounterStoreSelectors,
  useCounterStoreEvents,
  useCounterStore,
  CounterStoreContext
] = createStoreFormula(CounterStore)

Usage:

///// App.tsx

function App() {
  return (
    <CounterStoreProvider>
      <Counter />
    </CounterStoreProvider>
  )
}

function Counter() {
  const { increment, decrement, reset } = useCounterStoreEvents()

  return (
    <div>
      <br /><br />

      <CountDisplay />

      <button onClick={() => increment()}>Add</button>
      <button onClick={() => decrement()}>Subtract</button>
      <button onClick={() => reset()}>Reset</button>
    </div>
  )
}

function CountDisplay() {
  const count = useCounterStoreSelectors().use$count()

  return (
    <h5>Count: {count}</h5>
  )
}

You can also pass another store to props:

//// AnotherStore.tsx
type AnotherStoreProps = StoreProps<[StoreValue<typeof CounterStore>]>

export const anotherStoreDeps = [CounterStoreContext]

export function SecondStore({ onMount, onUnmount, stores: [counterStore] }) {
  const sub = counterStore.$count.watch((c: number) => {
    console.log("The count from SecondStore is: ", c)
  })

  onUnmount(() => {
    sub.unsubscribe()
  })

  //....
}


//// App.tsx
function App() {
  return (
    <CounterStoreProvider>
      <SecondStoreProvider contexts={anotherStoreDeps}>
        <Counter />
      </SecondStoreProvider>
    </CounterStoreProvider>
  )
}

Installation

npm install effector-formula

License

MIT

effector-formula's People

Contributors

adamkaraf avatar akisarou avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.