|
18 | 18 |
|
19 | 19 | * [Introduction](#introduction) |
20 | 20 | * [Installation](#installation) |
21 | | -* [Usage](#usage) |
| 21 | +* [Everyday Usage](#everyday-usage) |
22 | 22 | + [Creating global stores](#creating-global-stores) |
23 | 23 | + [Creating reactive views](#creating-reactive-views) |
24 | 24 | + [Creating local stores](#creating-local-stores) |
| 25 | +* [Advanced Usage](#advanced-usage) |
25 | 26 | + [Adding side effects](#adding-side-effects) |
| 27 | +* [API Summary](#api-summary) |
| 28 | + + [store(obj)](#storeobj) |
| 29 | + + [view(Comp)](#viewcomp) |
| 30 | + + [batch(fn)](#batchfn) |
| 31 | + + [autoEffect(fn)](#autoeffectfn) |
| 32 | + + [clearEffect(fn)](#cleareffectfn) |
26 | 33 | * [Examples with live demos](#examples-with-live-demos) |
27 | 34 | * [Articles](#articles) |
28 | 35 | * [Performance](#performance) |
@@ -79,7 +86,7 @@ _You need npm 5.2+ to use npx._ |
79 | 86 |
|
80 | 87 | </details> |
81 | 88 |
|
82 | | -## Usage |
| 89 | +## Everyday Usage |
83 | 90 |
|
84 | 91 | ### Creating global stores |
85 | 92 |
|
@@ -327,7 +334,7 @@ export default view(() => ( |
327 | 334 |
|
328 | 335 | ```jsx |
329 | 336 | import React from 'react'; |
330 | | -import { view, store, batch } from '@risingstack/react-easy-state'; |
| 337 | +import { view, store } from '@risingstack/react-easy-state'; |
331 | 338 |
|
332 | 339 | const user = store({ name: 'Bob', age: 30 }); |
333 | 340 |
|
@@ -626,6 +633,8 @@ Instead of returning an object, you should directly mutate the received stores. |
626 | 633 |
|
627 | 634 | </details> |
628 | 635 |
|
| 636 | +## Advanced Usage |
| 637 | + |
629 | 638 | ### Adding side effects |
630 | 639 |
|
631 | 640 | Use `autoEffect` to react with automatic side effect to your store changes. Auto effects should contain end-of-chain logic - like changing the document title or saving data to LocalStorage. `view` is a special auto effect that does rendering. |
@@ -727,6 +736,80 @@ class App extends Component { |
727 | 736 |
|
728 | 737 | --- |
729 | 738 |
|
| 739 | +## API Summary |
| 740 | + |
| 741 | +### store(obj) |
| 742 | + |
| 743 | +Creates an observable store from the passed object and returns it. Can be used outside components for [global stores](#creating-global-stores) and inside components for [local stores]((#creating-local-stores)). |
| 744 | + |
| 745 | +```js |
| 746 | +import { store } from '@risingstack/react-easy-state'; |
| 747 | + |
| 748 | +const user = store({ name: 'Rick' }); |
| 749 | +``` |
| 750 | + |
| 751 | +### view(Comp) |
| 752 | + |
| 753 | +Creates a [reactive view](#creating-reactive-views) from the passed component and returns it. A reactive view re-renders whenever any store data used inside it is mutated. |
| 754 | + |
| 755 | +```jsx |
| 756 | +import React from 'react'; |
| 757 | +import { view, store } from '@risingstack/react-easy-state'; |
| 758 | + |
| 759 | +const user = store({ name: 'Bob' }); |
| 760 | + |
| 761 | +export default view(() => ( |
| 762 | + <div>Hello {user.name}!</div> |
| 763 | +)); |
| 764 | +``` |
| 765 | + |
| 766 | +### batch(fn) |
| 767 | + |
| 768 | +Immediately executes the passed function and batches all store mutations inside it. Batched mutations are guaranteed to not trigger unnecessary double renders. Most task sources are batched automatically, only use `batch` if you encounter performance issues. |
| 769 | + |
| 770 | +```jsx |
| 771 | +import React from 'react'; |
| 772 | +import { view, store } from '@risingstack/react-easy-state'; |
| 773 | + |
| 774 | +const user = store({ name: 'Bob' }); |
| 775 | + |
| 776 | +function setName() { |
| 777 | + batch(() => { |
| 778 | + user.name = 'Rick' |
| 779 | + user.name = 'Ann' |
| 780 | + }) |
| 781 | +} |
| 782 | +``` |
| 783 | + |
| 784 | +### autoEffect(fn) |
| 785 | + |
| 786 | +Creates a reactive function from the passed one, immediately executes it, and returns it. A reactive function automatically re-reruns whenever any store data used inside it is mutated. |
| 787 | + |
| 788 | +Can be used both [outside](#global-auto-effects) and [inside](#local-auto-effects-in-function-components) components. |
| 789 | + |
| 790 | +```js |
| 791 | +import { store, autoEffect } from '@risingstack/react-easy-state'; |
| 792 | + |
| 793 | +const user = store({ name: 'Bob' }) |
| 794 | + |
| 795 | +autoEffect(() => document.title = user.name) |
| 796 | +``` |
| 797 | + |
| 798 | +### clearEffect(fn) |
| 799 | + |
| 800 | +Takes a reactive function (returned by `autoEffect`) and clears the reactivity from it. Cleared reactive functions will no longer re-rerun on related store mutations. Reactive functions created inside function components are automatically cleared when the component unmounts. |
| 801 | + |
| 802 | +```js |
| 803 | +import { store, autoEffect, clearEffect } from '@risingstack/react-easy-state'; |
| 804 | + |
| 805 | +const user = store({ name: 'Bob' }) |
| 806 | + |
| 807 | +const effect = autoEffect(() => document.title = user.name) |
| 808 | +clearEffect(effect) |
| 809 | +``` |
| 810 | + |
| 811 | +--- |
| 812 | + |
730 | 813 | ## Examples with live demos |
731 | 814 |
|
732 | 815 | #### Beginner |
|
0 commit comments