@shymean/react-vue-store
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

react-vue-store

定义store

import {createStoreInstance, defineStore} from '@shymean/react-vue-store'

const instance = createStoreInstance() // 不同store可以复用同一个instance

export const useMainStore = defineStore({
  id: 'test',
  instance,
  state(): { x: number } {
    return {
      x: 1,
    }
  },
  getters: {
    doubleX(): number {
      return 2 * this.x
    }
  },
  actions: {
    addX() {
      this.x += 1
    }
  },
})

消费、使用state

const SubApp = () => {
  const store = useMainStore()

  return () => {
    return (<div>
      store x is: {store.x}
    </div>)
  }
}

function App() {
  const store = useMainStore()
  const count = ref(0)

  const add = () => {
    store.addX()
  }

  return () => {
    return (<div>
      <button onClick={add}>click {count.value}</button>
      <SubApp/>
    </div>)
  }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @shymean/react-vue-store

Weekly Downloads

8

Version

1.0.7

License

ISC

Unpacked Size

4.56 kB

Total Files

5

Last publish

Collaborators

  • shymean