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

react-redux-presenters

v0.0.4

Published

Functional library for separating state & behavior from react components

Downloads

13

Readme

react-redux-presenters

Functional library for separating state & behavior from react components

This library aims to help keep your React components clean by providing a consistent pattern for managing redux mapStateToProps/mapDispatchToProps boilerplate.

Currently this library only contains a couple primitives, but as we gather more use cases, we will be adding more.

Presenters

The presenter pattern (also called "supervising controller") abstracts the management of view data & behavior to a separate module. The view itself is responsible for directly responding to user interaction, but it then delegates to the presenter for performing updates based on this interaction. This separation makes complex components easier to understand and makes it easier to test complex logic.

This library implements this pattern for your react components. It is based on, and assumes the use of, react-redux. The presenter will import the connect function from react-redux and build the mapStateToProps and mapDispatchToProps that you would normally do in the component.

Implementation

The basic implementation, without using this library, looks like:

import { connect } from 'react-redux';
import { toggleTodo } from '../actions';

const getVisibleTodos = (todos, filter) => {
  switch (filter) {
    case 'SHOW_ALL':
      return todos
    case 'SHOW_COMPLETED':
      return todos.filter(t => t.completed)
    case 'SHOW_ACTIVE':
      return todos.filter(t => !t.completed)
  }
}

const mapStateToProps = state => {
  return {
    todos: getVisibleTodos(state.todos, state.visibilityFilter)
  }
}

const mapDispatchToProps = dispatch => {
  return {
    onTodoClick: id => {
      dispatch(toggleTodo(id))
    }
  }
}

export default {
  connect: connect(mapStateToProps, mapDispatchToProps)
};

Then you can connect your component to the presenter like so:

import React from 'react';
import presenter from './presenter';

function VisibleTodos({ todos, onTodoClick }) {
  return (
    // Markup for Todo list here
  );
}

export default presenter.connect(VisibleTodos);

This Library

So why have a library?

  1. There are a number of different ways to structure data in a redux store. We want to provide abstractions to make creating presenters for these structures easy.
  2. Sometimes you may need to reuse the same presenter patterns in multiple presenters; the functional answer to reusability is composition. Therefore, this library provides a helper that allows you create presentational primitives and compose them into a single presenter.

Contributing

This library currently is very limited. It merely provides the composePresenters function and a helper for creating presenters if you use the normalizer format of structuring your redux store. If you are interested in helping out with this please drop me a line (I have contact info in my profile) or open an issue/PR explaining your use case and how we might be able to make it easier.