Skip to content

Simplify Api #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand All @@ -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() {
Expand All @@ -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',
Expand Down Expand Up @@ -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) => ({
Expand Down
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
2 changes: 1 addition & 1 deletion example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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