GithubHelp home page GithubHelp logo

joefiorini / react-redux-presenters Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 23 KB

Functional library for separating state & behavior from react components

Home Page: https://github.com/joefiorini/react-redux-presenters

JavaScript 100.00%
react redux presenters ui javascript react-redux

react-redux-presenters's Introduction

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.

react-redux-presenters's People

Contributors

joefiorini avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.