All Projects → lastmjs → Redux Store Element

lastmjs / Redux Store Element

Licence: mit
A custom element allowing a more declarative use of Redux.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Redux Store Element

vaadin-text-field
The themable Web Component providing input controls. Part of the Vaadin components.
Stars: ✭ 29 (-65.06%)
Mutual labels:  polymer, webcomponents, custom-elements
paper-chip
A chip web component made with Polymer 2 following Material Design guidelines
Stars: ✭ 30 (-63.86%)
Mutual labels:  polymer, webcomponents, custom-elements
Awesome Polymer
A collection of awesome Polymer resources.
Stars: ✭ 384 (+362.65%)
Mutual labels:  webcomponents, polymer, custom-elements
lego
🚀 Web-components made lightweight & Future-Proof.
Stars: ✭ 69 (-16.87%)
Mutual labels:  polymer, webcomponents, custom-elements
Vaadin Combo Box
The Web Component for displaying a list of items with filtering. Part of the Vaadin components.
Stars: ✭ 113 (+36.14%)
Mutual labels:  webcomponents, polymer, custom-elements
toggle-icon
toggle-icon is a custom element created with Polymer. It provides an extremely powerful and customizable switch that looks like a paper-icon-button.
Stars: ✭ 21 (-74.7%)
Mutual labels:  polymer, webcomponents, custom-elements
Login Fire
An element that allows simple configuration of multiple provider login for firebase
Stars: ✭ 58 (-30.12%)
Mutual labels:  webcomponents, polymer
Apollo Elements
🚀🌛 Use the Launch Platform 👩‍🚀👨‍🚀
Stars: ✭ 278 (+234.94%)
Mutual labels:  webcomponents, polymer
Snuggsi
snuggsi ツ - Easy Custom Elements in ~1kB
Stars: ✭ 288 (+246.99%)
Mutual labels:  webcomponents, custom-elements
Vaadin Grid
vaadin-grid is a free, high quality data grid / data table Web Component. Part of the Vaadin components.
Stars: ✭ 383 (+361.45%)
Mutual labels:  webcomponents, custom-elements
polymerx-cli
⚡ Unlock the power of Polymer 3, Web Components and modern web tools.
Stars: ✭ 30 (-63.86%)
Mutual labels:  polymer, webcomponents
Vaadin Core
An evolving set of free, open source web components for building mobile and desktop web applications in modern browsers.
Stars: ✭ 382 (+360.24%)
Mutual labels:  webcomponents, polymer
Web Components Angular React
Multiple apps as components POC
Stars: ✭ 64 (-22.89%)
Mutual labels:  webcomponents, custom-elements
Switzerland
🇨🇭Switzerland takes a functional approach to Web Components by applying middleware to your components. Supports Redux, attribute mutations, CSS variables, React-esque setState/state, etc… out-of-the-box, along with Shadow DOM for style encapsulation and Custom Elements for interoperability.
Stars: ✭ 261 (+214.46%)
Mutual labels:  webcomponents, custom-elements
anywhere-webcomponents
A UI work in progress based on custom elements (web components) for use in anywhere.
Stars: ✭ 17 (-79.52%)
Mutual labels:  webcomponents, custom-elements
Google Chart
Google Charts API web components
Stars: ✭ 284 (+242.17%)
Mutual labels:  webcomponents, polymer
Crab
JavaScript library for building user interfaces with Custom Elements, Shadow DOM and React like API
Stars: ✭ 22 (-73.49%)
Mutual labels:  webcomponents, custom-elements
Vanilla Colorful
A tiny color picker custom element for modern web apps (2.7 KB) 🎨
Stars: ✭ 467 (+462.65%)
Mutual labels:  webcomponents, custom-elements
Dna
Progressive Web Components.
Stars: ✭ 22 (-73.49%)
Mutual labels:  webcomponents, custom-elements
Gwt Api Generator
Generator for creating GWT JSInterop clients from Polymer Web Components
Stars: ✭ 49 (-40.96%)
Mutual labels:  webcomponents, polymer

CircleCI npm version dependency Status devDependency Status Published on webcomponents.org

<redux-store>

A custom element allowing a more declarative use of Redux.

Introduction

This custom element offers the basic Redux API declaratively. Because this is a custom element based on the web components standards, it should be compatible with all major front-end JavaScript frameworks and libraries that interoperate with custom elements, including Polymer, SkateJS, Vue.js, Angular, vanilla JavaScript, etc. See here for more information on interoperability of other frameworks and libraries with custom elements.

Simple and Declarative

Before we begin, I just want to highlight how easy it is to work with this element declaratively (using Polymer data binding syntax):

  • Hook up your root reducer (similar to Redux createStore):

    // HTML
    <redux-store root-reducer="[[rootReducer]]"></redux-store>
    
    // JS
    import {RootReducer} from '../../redux/reducers';
    
    ...
    
    connectedCallback() {
      this.rootReducer = RootReducer;
    }
    
  • Dispatch actions (similar to Redux dispatch):

    // HTML
    <redux-store action="[[action]]"></redux-store>
    
    // JS
    fireAnAction() {
      this.action = {
        type: 'CHANGE_THE_STATE'
      };
    }
    
  • Listen for state changes (similar to Redux subscribe):

    // HTML
    <redux-store on-statechange="stateChange"></redux-store>
    
    [[valueToBind]]
    
    // JS
    stateChange(e) {
      const state = e.detail.state;
    
      this.valueToBind = state.valueToBind;
    }
    
  • Explicitly grab the state (similar to Redux getState):

    // HTML
    <redux-store id="redux-store-element"></redux-store>
    
    // JS
    getReduxState() {
      const reduxStoreElement = this.querySelector('#redux-store-element');
      const state = reduxStoreElement.getState();
    }
    

Installation and Setup

Run the following:

npm install redux-store-element

Now import redux-store-element.js:

// HTML
<script type="module" src="node_modules/redux-store-element/redux-store-element.js">

// JavaScript

import 'redux-store-element';

This custom element depends on the custom elements and HTML imports web component specifications, which are not supported by all browsers yet. Include the webcomponentjs polyfills to ensure support across all modern browsers:

// CONSOLE
npm install --save @webcomponents/webcomponentsjs

// HTML
<script src="node_modules/webcomponentsjs/webcomponents-lite.js"></script>

This custom element also depends on native ES Modules support and bare specifier support. Use a bundler like Rollup or Webpack if your environment doesn't support ES Modules. If your environment does support ES Modules and you do not wish to use a bundler, you will need to use a static file server that provides rewrites for bare specifiers like Polyserve or Zwitterion.

The following examples are written with Polymer. It shouldn't be too hard to adapt them to other libraries and frameworks, keeping in mind their data binding systems and DOM interactions. This documentation is outdated, using HTML Imports instead of ES Modules, and will be updated in the future:

Creating the root reducer

At some point before you begin dispatching actions, you need to pass in your root reducer to any <redux-store></redux-store> element through the root-reducer attribute. From the example:

<link rel="import" href="../../../lib/bower_components/polymer/polymer.html">

<link rel="import" href="../../../src/redux-store.html">
<link rel="import" href="../input-area/input-area.component.html">
<link rel="import" href="../text/text.component.html">

<dom-module id="test-app">
    <template>
        <redux-store root-reducer="[[rootReducer]]"></redux-store>
        <test-input-area></test-input-area>
        <test-text></test-text>
    </template>

    <script>
        Polymer({
            is: 'test-app',
            ready: function() {

                var initialState = {
                    temp: 'initial temp'
                };

                this.rootReducer = function(state, action) {

                    if (!state) {
                        return initialState;
                    }

                    switch(action.type) {
                        case 'CHANGE_TEMP': {
                            var newState = Object.assign({}, state);

                            newState.temp = action.newTemp;

                            return newState;
                        }
                        default: {
                            return state;
                        }
                    };
                };

            }
        });
    </script>
</dom-module>

Subscribing to state changes

If your component needs to listen to state changes, simply pop a <redux-store></redux-store> element in and pass a listener function name in for the statechange event. From the example:

<link rel="import" href="../../../lib/bower_components/polymer/polymer.html">

<link rel="import" href="../../../src/redux-store.html">

<dom-module id="test-text">
    <template>
        <redux-store on-statechange="mapStateToThis"></redux-store>

        <div id="testText">Text from input above will go here</div>
    </template>

    <script>
        Polymer({
            is: 'test-text',
            mapStateToThis: function(e) {
                this.$.testText.innerHTML = e.detail.state.temp;
            }
        });
    </script>
</dom-module>

Dispatching actions

To dispatch from within an element, first bind the action property of the element to the action property on <redux-store></redux-store>. When you are ready to dispatch an action, set the action property on the element to the action that you want to dispatch. From the example:

<link rel="import" href="../../../src/redux-store.html">

<dom-module id="test-input-area">
    <template>
        <redux-store action="[[action]]"></redux-store>

        <input id="testInput" type="text">
        <button on-click="handleClick">Dispatch</button>
    </template>

    <script>
        Polymer({
            is: 'test-input-area',
            handleClick: function(e) {
                this.action = {
                    type: 'CHANGE_TEMP',
                    newTemp: this.$.testInput.value
                };
            }
        });
    </script>
</dom-module>

Multiple Stores

By default, there is one store for the entire application, meaning that each instance of a <redux-store></redux-store> will use the same store. You can however use multiple stores if you've determined it is necessary for your application. Simply reference the store name as follows:

  • Hook up your root reducer: <redux-store root-reducer="[[rootReducer]]" store-name="fooStore"></redux-store>
  • Dispatch actions: <redux-store action="[[action]]" store-name="fooStore"></redux-store>
  • Listen for state changes: <redux-store on-statechange="mapStateToThis" store-name="fooStore"></redux-store>

Important Things to Know:

  • Your runtime environment must support ES Modules natively. Check here for compatibility
  • You must pass in your root reducer to any <redux-store></redux-store> before actions will be dispatched and state change events raised
  • By default there is one store for the entire application. Each instance of a <redux-store></redux-store> will use the same store
  • The statechange event supplies the redux state in the detail.state property of the event

Development

npm install
npm run test-window
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].