Code Monkey home page Code Monkey logo

webcomponents-redux's Introduction

Web Components Redux

Web Components binding for Redux.

Quick Start Guide

Installation

Script Tag

<script src='https://unpkg.com/redux/dist/redux.min.js'></script>
<script src='https://unpkg.com/webcomponents-redux/dist/webcomponents-redux.min.js'></script>

ES or CommonJS Module

npm install --save redux webcomponents-redux

Web Components Redux Basics

For a Web Component call connect from webcomponents-redux package, passing the Web Component class and the Redux store object. The connect function adds Redux binding logic to the Web Component class.

import { connect } from 'webcomponents-redux';
connect(CounterElement, store);

Web Component class implements mapStateTopProps function to get notified for state change, and implements mapDispatchToProps for functions to dispatch actions.

There are two ways to integrate Redux into a Web Component.

Single Class Model

In a single class model, both UI and Redux logic is in one class. In the example CounterElement class below, mapStateToProps is implemented in the same class.

class CounterElement extends HTMLElement {
    attributeChangedCallback(name, oldValue, newValue) {
        if (name === 'value') {
            this.shadowRoot.innerHTML = `<div>Value is ${newValue}</div>`;
        }
    }

    mapStateToProps(oldState, newState) {
        if (oldState === undefined) {
            this.attributeChangedCallback('value', null, newState.counter.count);
            return;
        }

        if (newState.counter.count !== oldState.counter.count) {
            this.attributeChangedCallback('value', oldState.counter.count, newState.counter.count);
        }
    }
}

Presentation and Container Class Model

In this model, the presentation class only has the UI logic and Redux logic exist in Container class. This is shown in the example below.

class CounterElement extends HTMLElement {
    attributeChangedCallback(name, oldValue, newValue) {
        if (name === 'value') {
            this.countElement.innerText = newValue;
        }
    }
}

class CounterElementStateful extends CounterElement {
    mapStateToProps(oldState, newState) {
        if (oldState === undefined) {
            super.attributeChangedCallback('value', null, newState.counter.count);
            return;
        }

        if (newState.counter.count !== oldState.counter.count) {
            super.attributeChangedCallback('value', oldState.counter.count, newState.counter.count);
        }
    }
}

Sample Project

For complete example, see webcomponents-redux-sample repository for sample Web Coomponent implementation.

API Reference

connect

connect(class, store)

The connect function connects a Web Component to a Redux store.

Arguments

class: The Web Component class

store: The Redux store object

Returns

The function returns void.

Example

import { connect } from 'webcomponents-redux';

class CounterElement extends HTMLElement {
    // Component logic goes here
}

connect(CounterElement, store);

mapStateToProps

mapStateToProps(oldState, newState)

The Web Component class implements the mapStateToProps function to get notified of state change. The first time mapStateToProps function is called, is during connectedCallback lifecycle. After that on any state change, the mapStateToProps function is called.

Arguments

oldState: The old state object. The oldState is undefined, when mapStateToProps is called first time during connectedCallback lifecycle

newState: The new state object

Returns

The function returns void.

Example

mapStateToProps(oldState, newState) {
    if (oldState === undefined) {
        this.attributeChangedCallback('value', null, newState.counter.count);
        return;
    }

    if (newState.counter.count !== oldState.counter.count) {
        this.attributeChangedCallback('value', oldState.counter.count, newState.counter.count);
    }
}

mapDispatchToProps

mapDispatchToProps(dispatch)

The Web Component class implements the mapDispatchToProps function. The mapDispatchToProps function is only called one time during connectedCallback lifecycle, and should return an object, where each field of the object is a function, which is expected to dispatch an action to the store.

Arguments

dispatch: Redux store's dispatch function

Returns

Returns an object, where each field of the object is a function, which is expected to dispatch an action to the store.

Example

mapDispatchToProps(dispatch) {
    return {
        increment: () => dispatch({ type: 'INCREMENT' }),
        decrement: () => dispatch({ type: 'DECREMENT' }),
    };
}

Related Resources

License

MIT

webcomponents-redux's People

Contributors

sheeshpaul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

webcomponents-redux's Issues

Handling connectedCallback and disconnectedCallback

I had a read through the code. It's all pretty straight forward and makes good sense. However, the way that the connected/disconnected functionality is mixed in seems a bit odd, and also not accounting for a common scenario where many if not all web components will define those callbacks.

If the callback is present, could we instead wrap the original? Also, can we get away from leveraging __proto in the mixin implementations?

Other than those two issues, this looks pretty solid. Thanks for working on it!

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.