npm.io
2.0.11 • Published 2 years ago

build-plugin-ice-store

Licence
MIT
Version
2.0.11
Deps
10
Size
48 kB
Vulns
0
Weekly
0
Stars
18.6K

plugin-store

builtin icestore in icejs

Usage

Directory structure

src
├── models                // global models
│   └── user.ts
└── pages
    ├── About
    │   ├── index.tsx
    │   └── model.ts     // single model
    ├── Dashboard
    │   ├── analysis.tsx
    │   ├── index.tsx
    │   └── models         // multi models
    │       ├── modelA.ts
    │       └── modelB.ts
    └── index.tsx
Define a model
// src/models/user.ts
export default {
  state: {
    user: {}
  },

  actions: {
    getUserInfo: async () => {}
  }
};
With Component
import { store } from 'ice/Home'

const View = () => {
  const [state, actions] =  store.useModel('user')
  // do something...
}
Config

Set global initialstates to src/app.ts:

import { runApp } from 'ice'

const appConfig = {
  // Set global initialstates
  store: {
    initialStates: {}
  }
}

Set page initialstates to src/pages/*/index.tsx:

const HomePage = () => {
  return (
    <>
      <h2>HomePage</h2>
    </>
  )
}

HomePage.pageConfig = {
  // Set page initialstates
  initialstates: {}
}

More