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-ren

v1.0.0-alpha4

Published

Write react-redux without boilerplate.

Downloads

10

Readme

redux-ren

Write react-redux without actions boilerplate.

Consider this an experimental proof of concept.

redux-ren takes a functional stateless react component, enriches simple state modifier functions to become redux actions and sets everything up in the background so you end up with a working react-redux+thunk app.

The following is a rewrite of the original redux counter example using redux-ren. While this is one blob of JavaScript, the Counter component and the actions are completely independent and could be moved to individual files.

import ren from 'redux-ren';
import React from 'react';
import PropTypes from 'prop-types';

const Counter = (props) => (
  <p>
    Clicked: {props.value} times
      {' '}
    <button onClick={props.increment}>
      +
      </button>
    {' '}
    <button onClick={props.decrement}>
      -
      </button>
    {' '}
    <button onClick={props.incrementIfOdd}>
      Increment if odd
      </button>
    {' '}
    <button onClick={props.incrementAsync}>
      Increment async
      </button>
  </p>
);

Counter.propTypes = {
  value: PropTypes.number.isRequired,
  increment: PropTypes.func.isRequired,
  decrement: PropTypes.func.isRequired,
  incrementIfOdd: PropTypes.func.isRequired,
  incrementAsync: PropTypes.func.isRequired
};

// because these two actions increment "value" based on
// the existing state, we need to use (p, state) -
// "p" is just a dummy in this case because there's no payload
// for the action so we can get "state" as the second argument.
// the functions return an object with your intended state change.
// internally this will be handled then using
// const newState = { ...state, { value: state.value + 1 } };
const increment = (p, state) => ({ value: state.value + 1 });
const decrement = (p, state) => ({ value: state.value - 1 });

// the two following actions demonstrate how to use the thunk middleware.
// the outer function gets passed "actions" which are the actions we
// defined here wrapped with dispatchers. just calling "increment()"
// from above wouldn't have any effect, whereas "actions.increment()"
// correctly triggers the action tied to our react/redux setup.
// the inner function is a classic thunk. because we can access
// "actions" we don't really need dispatch, but calling
// dispatch({ type: 'increment' }) would work as well.
// in this case it's just there because we need "getState"
// as a second argument.
const incrementIfOdd = (actions) => (dispatch, getState) => {
  if (getState().value % 2 !== 0) {
    actions.increment();
  }
};

// this is another async action. In this case we can omit "(dispatch, getState)"
// altogether because we just want to trigger another action.
const incrementAsync = (actions) => () => {
  setTimeout(actions.increment, 1000);
};

const actions = { increment, decrement, incrementIfOdd, incrementAsync };
ren({ id: 'app', rootComponent: Counter, actions, initialState: { value: 0 } });