Code Monkey home page Code Monkey logo

react-event-store's Introduction

react-event-store

Simplified, typed, centralized event management for React

Demo

Usage

Firstly, define your events.

This is typed, centralized and organized.

// eventStore.ts
import { createEvent } from 'react-event-store'

export default {
  count: {
    add: createEvent<number>(),
    reset: createEvent(),
  },
  user: {
    logout: createEvent(),
    setting: createEvent(),
  },
}

Then, use useEvent hook listen to the events.

The listener will be automatically removed on unmount.

import { useEvent } from 'react-event-store'
import eventStore from './eventStore'

export default function Cart() {
  const [count, setCount] = React.useState(0)

  useEvent(eventStore.count.add, (by: number) => {
    setCount((count) => count + by)
  })

  return <div>I am going to buy {count} apples.</div>
}

Finally, trigger the events.

It's just like calling normal functions.

import eventStore from './eventStore'

return <button onClick={() => eventStore.count.add(5)}> +5 </button>

Advanced usage

If your event handlers need to read React states, you can use on/off manually.

React.useEffect(() => {
  function handleAdd(by: number) {
    setCount(count + by)
  }

  eventStore.count.add.on(handleAdd)
  return () => {
    eventStore.count.add.off(handleAdd)
  }
})

This is a little bit redundant, so it may be better to use signals instead.

npm install @preact/[email protected]

import { useSignal } from '@preact/signals-react'
import { useEvent } from 'react-event-store'
import eventStore from './eventStore'

export default function Cart() {
  const count = useSignal(0)

  useEvent(eventStore.count.add, (by: number) => {
    count.value += by
  })

  return (
    <div className='cart'>
      <div className='title'>Cart</div>
      <div>I am going to buy {count.value} apples.</div>
    </div>
  )
}

License

MIT

react-event-store's People

Contributors

jarvisniu 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.