main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1004 bytes
|
Line | |
---|
1 | Cursors
|
---|
2 | -------
|
---|
3 |
|
---|
4 | Cursors allow you to hold a reference to a path in a nested immutable data
|
---|
5 | structure, allowing you to pass smaller sections of a larger nested
|
---|
6 | collection to portions of your application while maintaining a central point
|
---|
7 | aware of changes to the entire data structure: an `onChange` function which is
|
---|
8 | called whenever a cursor or sub-cursor calls `update`.
|
---|
9 |
|
---|
10 | This is particularly useful when used in conjuction with component-based UI
|
---|
11 | libraries like [React](http://facebook.github.io/react/) or to simulate
|
---|
12 | "state" throughout an application while maintaining a single flow of logic.
|
---|
13 |
|
---|
14 |
|
---|
15 | ```javascript
|
---|
16 | var Immutable = require('immutable');
|
---|
17 | var Cursor = require('immutable/contrib/cursor');
|
---|
18 |
|
---|
19 | var data = Immutable.fromJS({ a: { b: { c: 1 } } });
|
---|
20 | var cursor = Cursor.from(data, ['a', 'b'], newData => {
|
---|
21 | data = newData;
|
---|
22 | });
|
---|
23 |
|
---|
24 | // ... elsewhere ...
|
---|
25 |
|
---|
26 | cursor.get('c'); // 1
|
---|
27 | cursor = cursor.update('c', x => x + 1);
|
---|
28 | cursor.get('c'); // 2
|
---|
29 |
|
---|
30 | // ... back to data ...
|
---|
31 |
|
---|
32 | data.getIn(['a', 'b', 'c']); // 2
|
---|
33 | ```
|
---|
Note:
See
TracBrowser
for help on using the repository browser.