Follow the steps below to use dob in react:
Create store and action -> combineStores
-> Provider
-> Connect
in component, and use all store&action
Create store and action
For example, we create ArticleStore
and ArticleAction
to manage articles.
@inject
will inject single same instance into any instance, which generated using combineStore
.
1 | import { observable, inject, Action } from "dob" |
Combine all of the actions and stores
As the above example shows, we combine ArticleStore
and ArticleAction
using combineStores
.
combineStores
will create single instance for each actions and stores, and help @inject
take effect.
1 | import { combineStores } from "dob" |
Use Provider in react root node
use Provider
in the root node, it’s an important step to inject stores to all child component.
Provider
will inject all the parameters it receives into the components that use the Connect
.
1 | import { Provider } from "dob-react" |
Use Connect in react component
finally, you can use all Actions and Stores in any react component that use Connect
.
@Connect
has two functions:
1. Pass all stores and actions to current component’s props, which registered in Provider
.
2. Auto rerender current component, when the store data it uses changed.
1 | import { Connect } from "dob-react" |
That’s all.