Skip to content

Commit 17f7539

Browse files
committed
docs(readme): add api docs
1 parent a86cb5f commit 17f7539

1 file changed

Lines changed: 86 additions & 3 deletions

File tree

README.md

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@
1818

1919
* [Introduction](#introduction)
2020
* [Installation](#installation)
21-
* [Usage](#usage)
21+
* [Everyday Usage](#everyday-usage)
2222
+ [Creating global stores](#creating-global-stores)
2323
+ [Creating reactive views](#creating-reactive-views)
2424
+ [Creating local stores](#creating-local-stores)
25+
* [Advanced Usage](#advanced-usage)
2526
+ [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)
2633
* [Examples with live demos](#examples-with-live-demos)
2734
* [Articles](#articles)
2835
* [Performance](#performance)
@@ -79,7 +86,7 @@ _You need npm 5.2+ to use npx._
7986

8087
</details>
8188

82-
## Usage
89+
## Everyday Usage
8390

8491
### Creating global stores
8592

@@ -327,7 +334,7 @@ export default view(() => (
327334

328335
```jsx
329336
import React from 'react';
330-
import { view, store, batch } from '@risingstack/react-easy-state';
337+
import { view, store } from '@risingstack/react-easy-state';
331338

332339
const user = store({ name: 'Bob', age: 30 });
333340

@@ -626,6 +633,8 @@ Instead of returning an object, you should directly mutate the received stores.
626633

627634
</details>
628635

636+
## Advanced Usage
637+
629638
### Adding side effects
630639

631640
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 {
727736

728737
---
729738

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+
730813
## Examples with live demos
731814

732815
#### Beginner

0 commit comments

Comments
 (0)