From a43f2f51aec69ab9508c96ab82498af5352f3f84 Mon Sep 17 00:00:00 2001 From: ArrayZoneYour Date: Thu, 6 Dec 2018 11:25:12 +0800 Subject: [PATCH] refactor(registerModel): fix the issue that must pass use... api to registerModel. --- dist/index.js | 6 ++++++ example/pages/index.tsx | 24 +++++------------------- src/index.tsx | 18 ++++++++++++++++-- 3 files changed, 27 insertions(+), 21 deletions(-) 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/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 }