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

js-redux

v2.0.0

Published

Redux bindings for vanilla javascript functions and classes

Downloads

31

Readme

js-redux Build Status npm version

Redux bindings for vanilla (no-framework) javascript functions and classes

This is a nice way to connect a function or class with Redux and associated actions + store reducers.

For the moment, it's really simple and the main logic is < 40 lines of code. It should be easy-to-understand assuming you are already familiar with Redux. If you have ideas for use-cases that this lib doesn't currently support then just open an issue.

Install

npm install --save js-redux

Provide a redux store

When you provide a store then it is provided globally (rather than being an instance). This is simple and supports my use-cases for the moment but feel free to send a PR if you'd like to add support for multiple provided instances.

import { createStore } from 'redux';
import { provide } from 'js-redux';
import rootReducer from './reducers';

const initialState = [];
const store = createStore(rootReducer, initialState);
provide(store);

Option 1: Function syntax

import { connect } from 'js-redux';
import * as todoActions from './actions'; // e.g. { addTodo, toggleTodo}

function _todos(state, actions) {
  // `state` is the initial selected state (state.todos) when the function is called
  // `actions` are dispatched like this: `actions.addTodo(...)`
  return {
    updated(newState, oldState) {
      // `updated` is called when selected state has been changed (shallowEquals)
    }
  };
}
export const todos = connect(state => state.todos, todoActions)(_todos)

// To execute your connected function just do:
todos();

Option 2: Class syntax

import { connect } from 'js-redux';
import * as todoActions from './actions';

@connect(state => state.todos, todoActions)
export class Todos {

  // `init` is called once when the class is initialized
  init() {
    // `this.state` is an up-to-date representation of the currently selected state
    // `this.actions` are dispatched like this: `this.actions.addTodo(...)`
  }

  // `updated` is called when selected state has been changed (shallowEquals)
  updated(oldState) {
    // `this.state` is the updated state. `oldState` is the previous state.
  }
}

// To init your connected class you can do either of (they both do the same thing):
Todos();
// or
new Todos();

API

provide(store)

Makes the Redux store available to the connect() calls below. You can’t use connect() without first providing a store for it to use. When you provide a store then it is 'provided' globally (rather than being an instance).

Arguments

  • store (Redux Store): The single Redux store in your application.

connect([mapStateToProps], [mapDispatchToProps])

Connects a React component to a Redux store. The connect API looks the same as connect in react-redux but it's not, it's far more basic.

It does not modify the class/function passed to it.
Instead, it returns a new, connected component class, for you to use.

Arguments

  • [mapStateToProps(state, [ownProps]): stateProps] (Function): If specified, the component will subscribe to Redux store updates. Any time it updates, updated will be called. Its result must be a plain object. It will be injected as either the first argument (for functions) — or this.state property (for classes).

  • [mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Object): Each function inside it will be assumed to be a Redux action creator. It injects an object with the same function names, but bound to a Redux store. It will be injected as either the second argument (for functions) — or this.action property (for classes).