Skip to content

Commit

Permalink
refactor(registerModel): fix the issue that must pass use... api to r…
Browse files Browse the repository at this point in the history
…egisterModel.
  • Loading branch information
ArrayZoneYour committed Dec 6, 2018
1 parent c85fcc8 commit a43f2f5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
6 changes: 6 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
24 changes: 5 additions & 19 deletions example/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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() {
Expand Down
18 changes: 16 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/// <reference path="./index.d.ts" />
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 = {}
Expand All @@ -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
}
Expand Down

0 comments on commit a43f2f5

Please sign in to comment.