Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A custom element allowing a more declarative use of Redux.

License

NotificationsYou must be signed in to change notification settings

lastmjs/redux-store-element

Repository files navigation

CircleCInpm versiondependency StatusdevDependency StatusPublished 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 theweb components standards, it should be compatible with all major front-end JavaScript frameworks and libraries that interoperate with custom elements, includingPolymer,SkateJS,Vue.js,Angular, vanilla JavaScript, etc. Seehere 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 ReduxcreateStore):

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

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

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

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

Installation and Setup

Run the following:

npm install redux-store-element

Now importredux-store-element.js:

// HTML<script type="module" src="node_modules/redux-store-element/redux-store-element.js">// JavaScriptimport '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:

// CONSOLEnpm 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 likeRollup orWebpack 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 likePolyserve orZwitterion.

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>    <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 thestatechange event. From the example:

<link rel="import" href="../../../lib/bower_components/polymer/polymer.html"><link rel="import" href="../../../src/redux-store.html"><dom-module>    <template>        <redux-store on-statechange="mapStateToThis"></redux-store>        <div>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>    <template>        <redux-store action="[[action]]"></redux-store>        <input 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. Checkhere 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
  • Thestatechange event supplies the redux state in thedetail.state property of the event

Development

npm installnpm run test-window

About

A custom element allowing a more declarative use of Redux.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp