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

redux-actuator

v2.0.1

Published

Communicate between components through Redux store

Downloads

6

Readme

Redux Actuator

Build Status

Trigger events inside components by reacting on pure state changes. Actuator keeps you from writing component lifecycle hooks boilerplate code by providing declarative API.

Motivation

In Redux apps subscribing to events inside components is typically considered as an anti-pattern. Components should be as pure as possible, meaning that they shouldn't have an internal state and they can only respond to props changes. This makes even more sense if we take into an account that React is "pull"-based: you don't have a control over rendering, you simply take the data and provide corresponding piece of DOM.

But there are cases when you want to comminicate with components in an imperative way. Imagine if you have an <IframePreview> component and you need to manually reload it when the button is clicked. One possible way to achieve that is to introduce an integer prop version and increment it on click.

The <IframePreview> external interface remains pure while its internal implementation uses componentDidUpdate callback in order to detect version changes and reload the iframe:

class IframePreview extends Component {
  componentDidUpdate (prevProps) {
    if (prevProps.version !== this.props.version) {
      this.reloadIframe()
    }
  }
}

When the app gets bigger the good idea is to move this counter to global state so you can control the iframe from where the bussiness logic resides. In Redux you have to declare action constant, write a reducer and action creator which isn't a problem for one component but becomes repetitive once you have more events and listening components. Redux Actuator aims to hide this logic and provide declarative API for event handling inside components.

Installation

Redux Actuator is distributed as an NPM package:

npm install --save redux-actuator
# or yarn add redux-actuator

Here how Redux Actuator is injected into Redux store:

  import createEngine from 'redux-actuator'

  const engine = createEngine()

  const reducer = combineReducers({
    // It's important for the reducer to be mounted at `actuator`
    // If you'd like to set up different mounting point see Configuration section.
    actuator: engine.reducer,

    // ... your other reducers
  })

That's it! Now we are ready to actuate!

Simpliest Actuator

One common use case for an actuator are state-free animations. Say, you want to animate this talking head:

In order to do that simply put <Actuator> component with channel provided.

  import { Actuator } from 'redux-actuator'

  class ThisGuy extends React.Component {
    saySomething (phrase) {
      // animation, DOM manipulation etc.
    }

    render () {
      // Actuator can wrap other components for brevity,
      // or can be used in a self-closing form <Actuator />
      return (
        <Actuator channel="talking-guy" on={(phrase) => this.saySomething(phrase)}>
          <div>
            ...
          </div>
        </Actuator>)
    }
  }

Now let's trigger actions from the bussiness-logic part of the app:

  import { actuate } from 'redux-actuator'

  // First argument is name of the event,
  // the rest is interpreted as event arguments
  store.dispatch(actuate('talking-guy', 'Hola!')
  store.dispatch(actuate('talking-guy', 'Hello!')

Subscribing to multiple channels. You can also subscribe to a multiple channels at once using this form:

  // Note: no need to pass a `channel` prop
  // Format is { chanOne: handler, chanTwo: ... }
  <Actuator on={{
    instantNotification: () => ..., 
    focusToolbar: () => ...
   }} />

License

Copyright 2017, Alexey Taktarov [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.