Detalhes do pacote

valtio-yjs

valtiojs2.2kMIT0.6.1

valtio-yjs makes yjs state easy

valtio, yjs, crdt

readme (leia-me)

valtio-yjs 💊🚀

CI npm size discord

valtio-yjs makes yjs state easy

What is this

valtio is a proxy state library for ReactJS and VanillaJS. yjs is an implementation of CRDT algorithm (which allows to merge client data without server coordination).

valtio-yjs is a two-way binding to bridge them.

Project status

It started as an experiment, and the experiment is finished. Now, it's in alpha. We encourage developers to try it in non-trivial apps, and find bugs.

Install

yarn add valtio-yjs valtio yjs

How to use it

import * as Y from 'yjs';
import { proxy } from 'valtio';
import { bind } from 'valtio-yjs';

// create a new Y doc
const ydoc = new Y.Doc();

// create a Y map
const ymap = ydoc.getMap('mymap');

// create a valtio state
const state = proxy({});

// bind them
const unbind = bind(state, ymap);

// now you can mutate the state
state.text = 'hello';

// you can nest objects
state.obj = { count: 0 };

// and mutate the nested object value
++state.obj.count;

// you can use arrays too
state.arr = [1, 2, 3];

// mutating the array is also possible
state.arr.push(4);

// unbind them by calling the result
unbind();

Demos

Using useSnapshot in valtio and WebsocketProvider in y-websocket, we can create multi-client React apps pretty easily.

changelog (log de mudanças)

Change Log

[Unreleased]

[0.6.1] - 2025-06-19

Changed

  • Fix: support mutation on the length property directly to shrink or extend arrays #51

[0.6.0] - 2024-06-04

Changed

  • Module-first setup #48

[0.5.1] - 2023-12-02

Changed

  • fix: guard against deleted parent and child #42

[0.5.0] - 2023-02-14

Added

  • refactor to use root level subscriptions #37

[0.4.1] - 2023-01-29

Changed

  • Use ymap value on nested map bind initialization #36

[0.4.0] - 2022-10-07

Changed

  • refactor: use bind api #34
    • [BREAKING CHANGE] bindProxyAndYMap and bindProxyAndYArray are combined to bind.

[0.3.1] - 2022-02-14

Changed

  • Skip non-proxy objects and only warn the usage (#23)

[0.3.0] - 2022-01-06

Changed

  • Pass transactionOrigin function through without calling it (#19)

[0.2.0] - 2021-12-17

Changed

  • Allow variable transactionOrigin value (#18)

[0.1.7] - 2021-12-13

Changed

  • fix: swapping array items (#8)

[0.1.6] - 2021-11-29

Changed

  • Fix infinite loop when replacing an array inside a nested map (#16)

[0.1.5] - 2021-11-28

Changed

  • Support nested map set (#15)

[0.1.4] - 2021-11-23

Changed

  • Add support for transactionOrigin (#13)

[0.1.3] - 2021-11-02

Changed

  • fix: initializing array with primitive values (#12)

[0.1.2] - 2021-10-05

Changed

  • fix: null values in object (#10)

[0.1.1] - 2021-08-18

Changed

  • fix: initializing array (#5)

[0.1.0] - 2021-07-25

Added

  • Initial alpha release