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 🙏

© 2024 – Pkg Stats / Ryan Hefner

dot-event-react

v1.3.0

Published

React integration for dot-event

Downloads

15

Readme

dot-event-react

React integration for dot-event.

dot-event-react

Install

npm install --save dot-event dot-event-react

What it does

This integration provides a React context provider and consumer that passes the dot-event instance down to any component.

Higher order components

We add the provider and the consumer to your component chain via higher order component "composers". Most of the examples below walk you through using the composers.

Re-render on emit

Additionally, any dot-event emit triggers a forceRender() from the provider, leaving it to your child components' shouldComponentUpdate() function to decide whether they should render.

Events composer

First we need a way to create the dot-event instance in a way that the provider can reuse.

Luckily dot-event provides a default events composer:

import Events from "dot-events"
Events.composer() // returns a new Events instance

The default events composer looks like this:

function composer(events = new Events()) {
  return events
}

Feel free to create your own events composer to customize the dot-event instance.

Events state

Dot-event takes a state option in its constructor:

import Events from "dot-events"
const events = new Events({ state: {} })
events.state // {}

The state object allows extensions like dot-store to persist state.

If you're using Next.js, the provider uses getInitialProps() to pass state from server to client.

Provider composer

Pass your events composer to withEventsProvider() to create a provider composer:

import Events from "dot-event"
import { withEventsProvider } from "dot-event-react"

export default withEventsProvider(Events.composer)

Add the consumer

Add the consumer to a component using withEvents() and pass it to the provider composer we created above:

import React from "react"
import { withEvents } from "dot-event-react"
import withEventsProvider from "./withEventsProvider"

class Component extends React.Component {
  componentDidMount() {
    const { events } = this.props
    events.on(async () => {})
  }
  render() {
    return null
  }
}

export default withEventsProvider(withEvents(Component))

Summary

  1. Create an events composer or use the default one (Events.composer)
  2. Pass the events composer to withEventsProvider() to create a provider composer
  3. Pass a component to withEvents() to add the consumer
  4. Pass the component with the consumer to the provider composer to create the final component chain

Read more about React context if you're not sure how the provider/consumer relationship works.

dot-store

Example with dot-store that takes advantage of server side state:

import React from "react"
import composeStore from "dot-store"
import {
  withEvents,
  withEventsProvider,
} from "dot-event-react"

const store = composeStore()

class Component extends React.Component {
  static async getInitialProps({ store }) {
    await store.set("message", "hello world")
  }
  render() {
    const { store } = this.props
    return store.get("message")
  }
}

export default withEventsProvider(store)(
  withEvents(Component)
)