diff --git a/README.md b/README.md index e4b5846..5423d89 100644 --- a/README.md +++ b/README.md @@ -28,23 +28,14 @@ npm install git+https://github.com/byte-fe/react-model.git#master react-model keep the state and actions in a global store. So you need to register them before using. ```typescript -import { useCallback, useContext, useEffect, useState } from 'react' -import { registerModel } from '../model' +import { registerModel } from 'react-model' import Home from '../model/home.model' import Shared from '../model/shared.model' -registerModel( - { - Home, - Shared - }, - { - useState, - useCallback, - useEffect, - useContext - } -) +registerModel({ + Home, + Shared +}) ``` ### useStore @@ -53,7 +44,7 @@ The functional component in React 16.7 can use Hooks to connect the global store ```javascript import React from 'react' -import { useStore } from '../model' +import { useStore } from 'react-model' export default () => { const [state, actions] = useStore('Home') @@ -80,7 +71,7 @@ The global state standalone can not effect the react class components, we need t ```jsx import { PureComponent } from 'react' -import { Provider } from './model' +import { Provider } from 'react-model' class App extends PureComponent { render() { @@ -101,7 +92,7 @@ Javascript decorator version ```jsx import React, { PureComponent } from 'react' -import { Provider, connect } from '../model' +import { Provider, connect } from 'react-model' const mapProps = ({ light, counter }) => ({ lightStatus: light ? 'open' : 'close', @@ -130,7 +121,7 @@ TypeScript Version ```tsx import React, { PureComponent } from 'react' -import { Provider, connect } from '../model' +import { Provider, connect } from 'react-model' import { StateType, ActionType } from '../model/home.model' const mapProps = ({ light, counter, response }: StateType) => ({ diff --git a/dist/index.js b/dist/index.js index 36fdea4..32151d7 100644 --- a/dist/index.js +++ b/dist/index.js @@ -75,6 +75,12 @@ var Setter = { // will throw Error Hooks can only be called inside the body of a function component var hooksApi = {}; var registerModel = function (models, hooks) { + if (hooks === void 0) { hooks = { + useCallback: react_1.useCallback, + useContext: react_1.useContext, + useEffect: react_1.useEffect, + useState: react_1.useState + }; } GlobalState = __assign({}, models); hooksApi = __assign({}, hooks); }; diff --git a/example/package-lock.json b/example/package-lock.json index 58f6ebd..79e7e6a 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -4306,7 +4306,7 @@ "integrity": "sha1-0ZhAioW0Bwk3qYZn9QDIMvhr1dQ=" }, "react-model": { - "version": "git+https://github.com/byte-fe/react-model.git#103c116cfabf205e24b470355976fc7134aa6f94", + "version": "git+https://github.com/byte-fe/react-model.git#a43f2f51aec69ab9508c96ab82498af5352f3f84", "from": "git+https://github.com/byte-fe/react-model.git#develop", "requires": { "react": "16.7.0-alpha.2", diff --git a/example/pages/index.tsx b/example/pages/index.tsx index dfe4cc0..e8b16df 100644 --- a/example/pages/index.tsx +++ b/example/pages/index.tsx @@ -1,10 +1,4 @@ -import React, { - PureComponent, - useCallback, - useContext, - useEffect, - useState -} from 'react' +import React, { PureComponent } from 'react' import { Provider, connect, registerModel } from 'react-model' import { StateType, ActionType } from '../model/home.model' import J from './counter' // JS Version @@ -13,18 +7,10 @@ import H from './hooks' import Home from '../model/home.model' import Shared from '../model/shared.model' -registerModel( - { - Home, - Shared - }, - { - useState, - useCallback, - useEffect, - useContext - } -) +registerModel({ + Home, + Shared +}) export default class App extends PureComponent { render() { diff --git a/src/index.tsx b/src/index.tsx index 00fbb2e..3357aff 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,6 +1,12 @@ /// import * as React from 'react' -import { PureComponent } from 'react' +import { + PureComponent, + useCallback, + useContext, + useEffect, + useState +} from 'react' import { GlobalContext, Consumer } from './helper' let GlobalState: any = {} @@ -14,7 +20,15 @@ let Setter = { // will throw Error Hooks can only be called inside the body of a function component let hooksApi: any = {} -const registerModel = (models: any, hooks: any) => { +const registerModel = ( + models: any, + hooks: any = { + useCallback, + useContext, + useEffect, + useState + } +) => { GlobalState = { ...models }