1.0.24 • Published 3 years ago

vare v1.0.24

Weekly downloads
28
License
-
Repository
-
Last release
3 years ago

Vare

Vue (Vue 3.x) Share State for a vue component

This is a Prerelease version!

This Vare works well, but there may be some unknown bugs.

What is this?

Vare works like Vuex.

However, this is less painful to create a Store than Vuex.

Use Vare with Vue (Vue 3.0 or Vue 2 with @vue/composition-api)

import {state} from './src/index'
import {defineComponent, h} from 'vue'

export const myState = state({
  name: 'foo',
})

export const FooComponent = defineComponent(() => {
  const name = computed(() => (myState.name))
  return () => {
    return h('span', name.value)
  }
})

State

import {state} from './src/index'
import {defineComponent, computed, h} from 'vue'

const myState = state({
  name: 'foo',
})

// using state in a components
export const FooComponent = defineComponent(() => {
  const name = computed(() => (myState.name))
  return () => {
    return h('span', name.value)
  }
})

Mutation

import {state, mutate} from './src/index'
import {defineComponent, h} from 'vue'


const myState = state({
  name: 'foo',
})

// mutation like Vuex Mutation
const setName = mutate((name: string) => {
  myState.name = name
})

// using state in a components
export const FooComponent = defineComponent(() => {
  const name = computed(() => (state.name))
  return () => {
    return h('div', [
      h('span', name.value),
      h('button', {onclick: () => setName('bar')}, 'click')
    ])
  }
})

Action

store/profile.ts

import {state, mutate, act} from './src/index'
import {h} from 'vue'

const myState = state({
  name: 'foo',
})

// mutation like Vuex Mutation
export const setName = mutate((name: string) => {
  myState.name = name
})

// action like Vuex Action
export const requestName = act((name: string) => {
  return Promise.resolve().then(() => {
    setName(name)
  })
})

export const FooComponent = defineComponent(() => {
  const name = computed(() => (state.name))

  return () => {
    return h('div', [
      h('span', name.value),
      h('button', {onclick: () => requestName('bar')}, 'click')
    ])
  }
})

Compute (Getter)

store/profile.ts

import {state, compute, mutate} from './src/index'
import {Ref, h, defineComponent, computed, ref} from 'vue'

const myState = state({
  name: 'foo',
})

// mutation like Vuex Mutation
export const setName = mutate((name: string) => {
  myState.name = name
})

export const getDecoName = compute(() => (`~~${myState.name}~~`))

export const getCustomDecoName = compute((deco: string) => `${deco}${myState.name}${deco}`)

export const getReactiveCustomDecoName = compute((deco: Ref<string>) => {
  return `${deco.value}${myState.name}${deco.value}`
})

export const FooComponent = defineComponent(() => {
  const decoName = getDecoName()
  const customDecoName = getCustomDecoName('++')
  const customDeco = ref('--')
  const customReactiveDecoName = getReactiveCustomDecoName(customDeco)

  function handleInput(event) {
    customDeco.value = event.target.value
  }

  return () => {
    return h('div',
      h(Fragment, [
        h('span', decoName.value), // ~~foo~~
        h('span', customDecoName.value), // ++foo++
        h('input', {onInput: handleInput, value: customDeco.value}), // --foo--
        h('button', {onclick: () => setName('bar')}, 'click')
      ])
    )
  }
})

Subscribe

import {state, mutate, act, subscribe, unsubscribe} from './src/index'
import {h} from 'vue'

const myState = state({
  name: 'foo',
})

// mutation like Vuex Mutation
export const setName = mutate((name: string) => {
  myState.name = name
})

// action like Vuex Action
export const requestName = act((name: string) => {
  return Promise.resolve().then(() => {
    setName(name)
  })
})

export const getDecoName = compute(() => (`~~${myState.name}~~`))()

const hook = () => {
  // any
}

subscribe(myState, hook)

subscribe(setName, hook)

subscribe(requestName, hook)

subscribe(getDecoName, hook)

unsubscribe(myState, hook)
unsubscribe(setName, hook)
unsubscribe(requestName, hook)
unsubscribe(getDecoName, hook)

Why

Share state wherever you want

Recoil x Vuex x Immer

Recoil

function App() {
  return (
    h(RecoilRoot, null,
      h(FooComponent),
      h(BarComponent)
    )
  )
}

// ....

const _state = atom({
  key: '...',
  default: {name: 'foo'}
})

const foo = selector({
  key: '...',
  get: ({get}) => {
    return get(_state).name
  },
  set: ({set, get}, name) => {
    set(_state, {
      ...get(_state),
      name,
    })
  }
})

function FooComponent() {
  const state = useRecoilValue(_state)
  
  return (
    h('div', null, state.foo)
  )
}

function BarComponent() {
  const setState = useSetRecoilState(foo)

  return (
    h('div', {onClick: () => setState('bar')})
  )
}

In the vare way

// no need a context

const myState = state({
  name: 'foo',
})


const getName = compute(() => myState.name)

const setName = mutate((name: string) => {
  state.foo = name
})

const App = defineComponent(() => {
  return () => h('div', [
    h(FooComponent),
    h(BarComponent)
  ])
})

const FooComponent = defineComponent(() => {
  const name = getName()
  
  return () => (
    h('div', name.value)
  )
})

const BarComponent = defineComponent(() => {
  return () => {
    h('div', {onclick: () => setName('bar')})
  }
})

Immer

const myState = {
  foo: 'foo'
}

produce(myState, (draft) => {
  draft.foo = 'bar'
})

In the Vare way

const myState = state({
  foo: 'foo',
})

myState.foo = 'bar'

Vuex

const store = createStore({
  state () {
    return {
      count: 0
    }
  },
  mutations: {
    increment (state) {
      state.count++
    }
  }
})

// need to setup
app.use(store)

const FooComponent = defineComponent(() => {
  const store = useStore()

  return () => (
    h('div', [
      h('div', store.state.count),
      h('a', {onClick: () => store.commit('increment')}),
    ])
  )
})

In the Vare way

// no need to setup

const myState = state({
  count: 0,
})

const increment = mutate(() => {
  myState.count++
})

const FooComponent = defineComponent(() => {
  const count = computed(() => (myState.count))

  return () => (
    h('div', [
      h('div', count.value),
      h('a', {onClick: increment}),
    ])
  )
})

Create the pack in easy way (WIP)

Supporting Vue DevTool ?

Nope! (WIP)

1.0.25-alpha.14

3 years ago

1.0.25-alpha.12

3 years ago

1.0.25-alpha.13

3 years ago

1.0.25-alpha.10

3 years ago

1.0.25-alpha.11

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.25-alpha.9

3 years ago

1.0.25-alpha.8

3 years ago

1.0.25-alpha.7

3 years ago

1.0.25-alpha.6

3 years ago

1.0.25-alpha.4

3 years ago

1.0.25-alpha.3

3 years ago

1.0.25-alpha.2

3 years ago

1.0.25-alpha.1

3 years ago

1.0.25-alpha.0

3 years ago

1.0.24-alpha.1

3 years ago

1.0.24-alpha.0

3 years ago

1.0.23-alpha.9

3 years ago

1.0.23-alpha.5

3 years ago

1.0.23-alpha.8

3 years ago

1.0.23-alpha.20

3 years ago

1.0.23-alpha.19

3 years ago

1.0.23-alpha.18

3 years ago

1.0.23-alpha.15

3 years ago

1.0.23-alpha.14

3 years ago

1.0.23-alpha.17

3 years ago

1.0.23-alpha.16

3 years ago

1.0.23-alpha.11

3 years ago

1.0.23-alpha.10

3 years ago

1.0.23-alpha.13

3 years ago

1.0.23-alpha.26

3 years ago

1.0.23-alpha.25

3 years ago

1.0.23-alpha.22

3 years ago

1.0.23-alpha.21

3 years ago

1.0.23-alpha.24

3 years ago

1.0.23-alpha.23

3 years ago

1.0.19

3 years ago

1.0.23-alpha.0

3 years ago

1.0.23-alpha.1

3 years ago

1.0.22

3 years ago

1.0.23-alpha.2

3 years ago

1.0.23-alpha.3

3 years ago

1.0.20

3 years ago

1.0.23-alpha.4

3 years ago

1.0.19-beta.9

3 years ago

1.0.19-beta.8

3 years ago

1.0.19-beta.6

3 years ago

1.0.19-beta.5

3 years ago

1.0.19-beta.4

3 years ago

1.0.19-beta.1

3 years ago

1.0.19-beta.0

3 years ago

1.0.19-beta.11

3 years ago

1.0.19-beta.10

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.19-alpha.0

3 years ago

1.0.17-alpha.8

3 years ago

1.0.17-alpha.7

3 years ago

1.0.17-alpha.6

3 years ago

1.0.17-alpha.5

3 years ago

1.0.17-alpha.4

3 years ago

1.0.17-alpha.3

3 years ago

1.0.17-alpha.2

3 years ago

1.0.17-alpha.1

3 years ago

1.0.16

3 years ago

1.0.16-next.19

3 years ago

1.0.16-next.18

3 years ago

1.0.16-next.17

3 years ago

1.0.16-next.16

3 years ago

1.0.16-next.15

3 years ago

1.0.16-next.13

3 years ago

1.0.16-next.12

3 years ago

1.0.16-next.11

3 years ago

1.0.16-next.8

3 years ago

1.0.16-next.6

3 years ago

1.0.16-next.5

3 years ago

1.0.16-next.2

3 years ago

1.0.16-next.3

3 years ago

1.0.16-next.0

3 years ago

1.0.16-next.1

3 years ago

1.0.15-next.7

3 years ago

1.0.15-next.6

3 years ago

1.0.15

3 years ago

1.0.9

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.3

4 years ago

1.0.0

4 years ago

1.0.0-beta.2

4 years ago

1.0.0-beta.1

4 years ago

1.0.0-beta.0

4 years ago

0.3.2-alpha.0

4 years ago

0.2.0-alpha.2

4 years ago

0.2.0-alpha.0

4 years ago

0.2.0-alpha.1

4 years ago

0.1.1-alpha.0

4 years ago

0.1.0

4 years ago

0.1.1

4 years ago

0.0.1-9

4 years ago

0.0.1-8

4 years ago

0.0.1-4

4 years ago