npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mobx-vue-lite

v0.4.3

Published

Lightweight Vue 3 bindings for MobX based on Composition API.

Downloads

6,796

Readme

mobx-vue-lite

Lightweight Vue 3 bindings for MobX based on Composition API.

Demo: https://stackblitz.com/edit/mobx-vue-lite-demo

Install

npm install mobx mobx-vue-lite

Example

useLocalObservable<T>(initializer: () => T, annotations?: AnnotationsMap<T>): Ref<T>

Creates an observable object with the given properties, methods and computed values.

<script setup>
import { useLocalObservable } from 'mobx-vue-lite'

const state = useLocalObservable(() => ({
    count: 0,
    get double() {
        return this.count * 2
    },
    increment() {
        this.count++
    }
}))
</script>

<template>
    <div>Count: {{ state.count }}</div>
    <div>Doubled: {{ state.double }}</div>
    <button @click="state.increment">Increment</button>
</template>

<Observer />

Is a renderless Vue component, which applies observer to its children.

Install as a global plugin (Optional)

// main.js
import { createApp } from 'vue'
import Observer from 'mobx-vue-lite'

const app = createApp(App)
app.use(Observer)

Or: Import and register it locally

<script setup>
import { observable, runInAction } from 'mobx'
import { Observer } from 'mobx-vue-lite'

const data = observable({ name: 'John' })

const changeName = () => {
    runInAction(() => {
        data.name = 'Jane'
    })
}
</script>

<template>
  <Observer>
    <div>Name: {{ data.name }}</div>
    <button @click="changeName">Change name</button>
  </Observer>
</template>

createGlobalObservable<T>(stateFactory: () => T): () => T

Create a global observer from a local observer.

// store.ts
import { createGlobalObservable, useLocalObservable } from 'mobx-vue-lite'

export const useGlobalObservable = createGlobalObservable(() => {
  return useLocalObservable(() => ({
    count: 0,
    get double() {
      return this.count * 2
    },
    increment() {
      this.count++
    },
  }))
})
<script setup>
import { useGlobalObservable } from './store'

// Can be reused in any component and state will be in sync
const state = useGlobalObservable()
</script>

<template>
  <div>Count: {{ state.count }}</div>
  <div>Doubled: {{ state.double }}</div>
  <button @click="state.increment">Increment</button>
</template>

Tips

You can watch the state and its changes through Vue's watch:

import { watch } from 'vue'

const state = useLocalObservable(() => ({
  count: 0,
  increment() {
    this.count++
  },
}))

// watch the whole state
watch(state, (value) => {
  console.log(value)
})

// watch part of a state
watch(() => state.value.count, (count) => {
  console.log(count)
})

Class observables should work out-of-the-box. Just wrap the component with the <Observer /> component.

<script setup>
import { Observer } from 'mobx-vue-lite'
import { CounterStore } from './counterStore'

const state = new CounterStore()
</script>

<template>
  <Observer>
    <h1 v-text="state.count" />
    <button @click="state.decrement">-</button>
    <button @click="state.increment">+</button>
  </Observer>
</template>

Usage with Nuxt 3

To use the <Observer /> component globally a Nuxt 3 app, add this to your nuxt config:

import { defineNuxtConfig } from 'nuxt3'

export default defineNuxtConfig({
  buildModules: ['mobx-vue-lite/nuxt'],
})

Credits

API was inspired from https://github.com/mobxjs/mobx-react-lite.

License

MIT