Coder Social home page Coder Social logo

vue-signals's Introduction

๐Ÿ“ถ vue-signals

NPM version

This package is a thin wrapper around Vue's shallowRef() function that exposes the same API as:

Technically speaking, Vue refs are already reactive signals. That's why it's easy to replicate the specific API design choices of other frameworks. For more details on the distinction between signals and refs, see the Vue docs on their connection.

Installation

To install the package, run:

pnpm install vue-signals # or npm or yarn

Usage

Solid

Import the createSignal() function from vue-signals/solid:

<script lang="ts" setup>
import { createSignal } from 'vue-signals/solid'

const [count, setCount] = createSignal(0)
</script>

<template>
  <p>Count: {{ count() }}</p>
  <button @click="setCount((v) => v + 1)">
    increment
  </button>
</template>

Angular

Import the signal() and computed() functions from vue-signals/angular:

<script lang="ts" setup>
import { computed, signal } from 'vue-signals/angular'

const count = signal(0)
const double = computed(() => count() * 2)
</script>

<template>
  <p>Count: {{ count() }}</p>
  <p>Double: {{ double() }}</p>
  <button @click="count.update(v => v + 1)">
    increment
  </button>
  <button @click="count.set(0)">
    reset
  </button>
</template>

API

Solid-Style createSignal<T>

type SignalGetter<T> = () => T
type SignalSetter<T> = (v: T | ((v: T) => T)) => void

type Signal<T = any> = [
  SignalGetter<T>,
  SignalSetter<T>
]

declare function createSignal<T = any>(
  value: T,
  { equals }?: { equals?: false | ((prev: T, next: T) => boolean) }
): Signal<T>

Angular-Style signal<T>

type Signal<T = any> = () => T & {
  set: (value: T) => void
  update: (updater: (value: T) => T) => void
  mutate: (mutator: (value: T) => void) => void
}

declare function signal<T = any>(initialValue: T): Signal
declare function computed<T>(getter: (...args: any[]) => T): () => T

FAQ

Why, Though?

Some users may prefer more explicit control over their reactive state (read/write access). For example no other code could trigger changes unless they have access to the setter.

Users new to Vue.js may be confused by the ref() API. It's not immediately obvious that ref() returns a reactive and mutable object. This is especially true for users coming from React, where useState() returns a tuple of [value, setter].

Credits

License

MIT License ยฉ 2023 Johann Schopplich

MIT License ยฉ 2023 Evan You

vue-signals's People

Contributors

johannschopplich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

kento97

vue-signals's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.