Skip to content

Commit e3d2bac

Browse files
Merge pull request #2 from byte-fe/develop
Simplify Api
2 parents d0a3bcb + 97a721f commit e3d2bac

File tree

5 files changed

+37
-40
lines changed

5 files changed

+37
-40
lines changed

README.md

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,14 @@ npm install git+https://github.com/byte-fe/react-model.git#master
2828
react-model keep the state and actions in a global store. So you need to register them before using.
2929

3030
```typescript
31-
import { useCallback, useContext, useEffect, useState } from 'react'
32-
import { registerModel } from '../model'
31+
import { registerModel } from 'react-model'
3332
import Home from '../model/home.model'
3433
import Shared from '../model/shared.model'
3534

36-
registerModel(
37-
{
38-
Home,
39-
Shared
40-
},
41-
{
42-
useState,
43-
useCallback,
44-
useEffect,
45-
useContext
46-
}
47-
)
35+
registerModel({
36+
Home,
37+
Shared
38+
})
4839
```
4940

5041
### useStore
@@ -53,7 +44,7 @@ The functional component in React 16.7 can use Hooks to connect the global store
5344

5445
```javascript
5546
import React from 'react'
56-
import { useStore } from '../model'
47+
import { useStore } from 'react-model'
5748

5849
export default () => {
5950
const [state, actions] = useStore('Home')
@@ -80,7 +71,7 @@ The global state standalone can not effect the react class components, we need t
8071

8172
```jsx
8273
import { PureComponent } from 'react'
83-
import { Provider } from './model'
74+
import { Provider } from 'react-model'
8475

8576
class App extends PureComponent {
8677
render() {
@@ -101,7 +92,7 @@ Javascript decorator version
10192

10293
```jsx
10394
import React, { PureComponent } from 'react'
104-
import { Provider, connect } from '../model'
95+
import { Provider, connect } from 'react-model'
10596

10697
const mapProps = ({ light, counter }) => ({
10798
lightStatus: light ? 'open' : 'close',
@@ -130,7 +121,7 @@ TypeScript Version
130121

131122
```tsx
132123
import React, { PureComponent } from 'react'
133-
import { Provider, connect } from '../model'
124+
import { Provider, connect } from 'react-model'
134125
import { StateType, ActionType } from '../model/home.model'
135126

136127
const mapProps = ({ light, counter, response }: StateType) => ({

dist/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ var Setter = {
7575
// will throw Error Hooks can only be called inside the body of a function component
7676
var hooksApi = {};
7777
var registerModel = function (models, hooks) {
78+
if (hooks === void 0) { hooks = {
79+
useCallback: react_1.useCallback,
80+
useContext: react_1.useContext,
81+
useEffect: react_1.useEffect,
82+
useState: react_1.useState
83+
}; }
7884
GlobalState = __assign({}, models);
7985
hooksApi = __assign({}, hooks);
8086
};

example/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/pages/index.tsx

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import React, {
2-
PureComponent,
3-
useCallback,
4-
useContext,
5-
useEffect,
6-
useState
7-
} from 'react'
1+
import React, { PureComponent } from 'react'
82
import { Provider, connect, registerModel } from 'react-model'
93
import { StateType, ActionType } from '../model/home.model'
104
import J from './counter' // JS Version
@@ -13,18 +7,10 @@ import H from './hooks'
137
import Home from '../model/home.model'
148
import Shared from '../model/shared.model'
159

16-
registerModel(
17-
{
18-
Home,
19-
Shared
20-
},
21-
{
22-
useState,
23-
useCallback,
24-
useEffect,
25-
useContext
26-
}
27-
)
10+
registerModel({
11+
Home,
12+
Shared
13+
})
2814

2915
export default class App extends PureComponent {
3016
render() {

src/index.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
/// <reference path="./index.d.ts" />
22
import * as React from 'react'
3-
import { PureComponent } from 'react'
3+
import {
4+
PureComponent,
5+
useCallback,
6+
useContext,
7+
useEffect,
8+
useState
9+
} from 'react'
410
import { GlobalContext, Consumer } from './helper'
511

612
let GlobalState: any = {}
@@ -14,7 +20,15 @@ let Setter = {
1420
// will throw Error Hooks can only be called inside the body of a function component
1521
let hooksApi: any = {}
1622

17-
const registerModel = (models: any, hooks: any) => {
23+
const registerModel = (
24+
models: any,
25+
hooks: any = {
26+
useCallback,
27+
useContext,
28+
useEffect,
29+
useState
30+
}
31+
) => {
1832
GlobalState = {
1933
...models
2034
}

0 commit comments

Comments
 (0)