npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-announce

v3.0.0

Published

a declarative approach to writing react components

Readme

react-announce

Build Status npm Coverage Status

A declarative approach to writing react components. Enables reuse of component behaviors.

Installation

npm i react-announce --save

API

@asStream

@asStream() decorator lets any observer subscribe to the lifecycle events (and also custom events that we will see later) of the component.

const Rx = require('rx')
const observerA = Rx.Observer.create(x => console.log('A', x))
const observerB = Rx.Observer.create(x => console.log('B', x))

@asStream(observerA, observerB)
class MyComponent extends Component {
  render () {
    return (<div>Hello World!</div>)
  }
}

/*
OUTPUT:

A {event: 'WILL_MOUNT', args: [], component: MyComponent {}}
B {event: 'WILL_MOUNT', args: [], component: MyComponent {}}
A {event: 'DID_MOUNT', args: [], component: MyComponent {}}
B {event: 'DID_MOUNT', args: [], component: MyComponent {}}

*/

Every subscriber gets all the notification from all the instances of the MyComponent class, of each of its lifecycle and custom events. Each notification on the stream is fired with three keys —

{
 "event": "WILL_MOUNT", /*DID_MOUNT, WILL_RECEIVE_PROPS, WILL_UPDATE, DID_UPDATE, WILL_UNMOUNT*/

 "args": [], /*the arguments with which the event was dispatched*/,

 "component": {} /*instance of the component*/
}

Too improve performance, the subscriptions are only created once the component mounts and are automatically removed (disposed) as soon as it unmounts.

getInstanceStream(stream: Observable, dispose: function)

Exposes the component events of only the CURRENT instance, as a stream and is always called with context of that instance. It is called as soon as the component is mounted.

@asStream()
class MyComponent extends Component {
  getInstanceStream (stream, dispose) {
    //{stream} exposes events of only the current instance of the component.
    dispose(stream.subscribe(x => console.log(x)))
  }
  render () {
    return (<div>Hello World!</div>)
  }
}

The second param is dispose which disposes the subscription as soon as the component unmounts. This frees up the allocated memory and improves performance.

dispatch(string: event, ...args)

The component gets a special function named dispatch() which enables us to dispatch custom events. This is a very powerful feature, as it removes the need of passing loads of callbacks to the deepest level of components. For example —

const bus = new Rx.Subject()

@asStream(bus)
class MyComponent extends Component {

  onClick (e) {
    /*
    * The first param is used as the name of the event
    * Rest of the params are passed as the `args` property to the component stream.
    **/
    this.dispatch('CLICKED', e)
  }

  render () {
    return (<div onClick={this.onClick.bind(this)}>Hello World!</div>)
  }
}

bus.subscribe(x => console.log(x))

Here the bus object can be accessed globally for any kind of internal event that is fired. There is no need of passing a callback to this component for the onClick event, instead I can directly subscribe to the bus.

IMPORTANT This functionality can soon become a problem if all the component's start exposing streams. Use this feature ONLY if a component has a lot of interactions built into it. For example — A form component which has multiple text boxes and dropdowns.

Extensions

You can create multiple extensions which are based on this module using the createDeclarative method. The method essentially helps you define a custom getInstanceStream method without the verbosity.

For instance if I want to create a timer declarative, that sets the time elapsed since component was mounted to the state of that particular component, then I do as follows —

const timer = createDeclarative(function (stream, dispose, interval, stateProperty) {
  const time = Observable.interval(interval).map(Date.now)
  dispose(
    stream.filter(x => x.event === 'WILL_MOUNT')
    .map(() => Date.now())
    .combineLatest(time, (t1, t2) => t2 - t1)
    .subscribe(x => this.setState({[stateProperty]: x}))
  )
})

/*
Usage
*/

@timer(100, 'elapsedTime')
class MyComponent extends Component {
  render () {
    return (<div>Time Elapsed: {this.state.elapsedTime}ms</div>)
  }
}
// Keeps printing the elapsed time which gets updated ever 100ms

createDeclarative uses @asStream declarative and sets its getInstanceStream method to your custom method.

Lifecycle Events

  • WILL_MOUNT
  • DID_MOUNT
  • WILL_RECEIVE_PROPS
  • WILL_UPDATE
  • DID_UPDATE
  • WILL_UNMOUNT

Release Channels

There are two release channels — @stable & @latest

npm i react-announce@stable --save
npm i react-announce@latest --save

Available Declaratives npm