1 |
|
2 | # virtex-local
|
3 |
|
4 | [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
|
5 |
|
6 | A local state middleware for [virtex](https://github.com/ashaffer/virtex) [components](https://github.com/ashaffer/virtex-component)
|
7 |
|
8 | ## Installation
|
9 |
|
10 | $ npm install virtex-local
|
11 |
|
12 | ## Usage
|
13 |
|
14 | First, you need to install it in your redux middleware stack *before* [virtex-component](https://github.com/ashaffer/virtex-component). E.g.
|
15 |
|
16 | `applyMiddleware(local('app'), component, dom(document), ...others)`
|
17 |
|
18 | The `'app'` string passed to virtex-local tells it where in your global state atom your component state tree will be mounted. In this case, `state.app` is where it will live. In order for this to work, you will also need to mount [redux-ephemeral](https://github.com/ashaffer/redux-ephemeral) into your reducer at the same key, like this:
|
19 |
|
20 | ```javascript
|
21 | import {mount} from 'virtex-local'
|
22 |
|
23 | export default mount('app', reducer)
|
24 | ```
|
25 |
|
26 | ### Local state
|
27 |
|
28 | When using virtex-local, your components will receive three extra things in their model:
|
29 |
|
30 | * `local` - Wraps actions that you want to direct to your local reducer.
|
31 | * `state` - Your components local state.
|
32 |
|
33 | ### Example
|
34 |
|
35 | ```javascript
|
36 | import element from 'virtex-element'
|
37 |
|
38 | function render ({local, state}) {
|
39 | return (
|
40 | <input type='text' onChange={local(setText)} />
|
41 | <span>
|
42 | The text in your input is {state.text}
|
43 | </span>
|
44 | )
|
45 | }
|
46 |
|
47 | const SET_TEXT = 'SET_TEXT'
|
48 |
|
49 | function setText (e) {
|
50 | const text = e.currentTarget.value
|
51 |
|
52 | return {
|
53 | type: SET_TEXT,
|
54 | payload: text
|
55 | }
|
56 | }
|
57 |
|
58 | function reducer (state, action) {
|
59 | switch (action.type) {
|
60 | case SET_TEXT:
|
61 | return {
|
62 | ...state,
|
63 | text: action.payload
|
64 | }
|
65 | }
|
66 |
|
67 | return state
|
68 | }
|
69 |
|
70 | export default {
|
71 | render,
|
72 | reducer
|
73 | }
|
74 | ```
|
75 |
|
76 | ## Dirty map
|
77 |
|
78 | There is a second argument you may pass to the virtex-local middleware when you install it, `dirty`. If you pass this argument, virtex-local will maintain a map of the dirty state of your components. This can be useful for implementing partial subtree re-rendering optimizations. E.g.
|
79 |
|
80 | ```javascript
|
81 | const dirty = {}
|
82 | applyMiddleware(local('app', dirty), ...)
|
83 |
|
84 | function update (state) {
|
85 | Object.keys(dirty).forEach(key => rerenderSubtree(key)))
|
86 | }
|
87 | ```
|
88 |
|
89 | ## License
|
90 |
|
91 | The MIT License
|
92 |
|
93 | Copyright © 2015, Weo.io <info@weo.io>
|
94 |
|
95 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
96 |
|
97 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
98 |
|
99 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|